-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
119 lines (96 loc) · 3.92 KB
/
app.py
File metadata and controls
119 lines (96 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import streamlit as st
import pandas as pd
from dotenv import load_dotenv
import os
from pandasai import SmartDatalake
from pandasai.llm import GooglePalm
import google.generativeai as genai
from streamlit_local_storage import LocalStorage
from PyPDF2 import PdfReader
load_dotenv()
history=[]
genai.configure(api_key=os.environ['API_KEY'])
localS = LocalStorage()
api = os.getenv('API_KEY')
model = genai.GenerativeModel('gemini-pro')
def chat_with_pdf(text, prompt):
pdf_text = ""
for data in text:
pdf_text += data
response = model.generate_content(prompt+" The pdf document text is given as: "+pdf_text)
print(response.text)
return response.text
def chat_with_csv(data,prompt):
llm = GooglePalm(api_key = api)
df = SmartDatalake(data,config={"llm":llm})
result = df.chat(prompt)
print(result)
return result
st.set_page_config(layout="wide")
st.title("ChatWithFiles powered by LLM")
all_csv = st.file_uploader("Upload your CSV file",type=['csv'], accept_multiple_files=True)
if all_csv:
selected_file = st.selectbox("Select a CSV file", [file.name for file in all_csv])
selected_index = [file.name for file in all_csv].index(selected_file)
col1,col2 = st.columns([1,1])
with col1:
st.info("CSV uploaded successfully")
data = []
for file in all_csv:
data.append(pd.read_csv(file))
st.dataframe(data[selected_index])
with col2:
st.info("Chat with your CSV")
input_text = st.text_area("Enter your query",key="csv_query") # check why 1st word not coming
if input_text is not None:
col1, col2 = st.columns([1,4]) # Create two columns
if col1.button("Ask Query", key="ask_query_csv"):
st.info("Your query:" + " " + input_text)
result = chat_with_csv(data, input_text)
st.download_button(label="Download Result as Text", data="Query: {}\nResult: {}".format(input_text, result), file_name="pdf_result.txt", mime="text/plain")
st.success(result)
if col2.button("Plot Graph"):
st.info("Your query:" + " " + input_text)
result = chat_with_csv(data[selected_index], input_text)
st.image('exports/charts/temp_chart.png')
def extract_text_from_pdf(pdf_file):
text = ""
with pdf_file as file:
reader = PdfReader(file)
for page in reader.pages:
st = page.extract_text()
text += st
return text
def displayHistory():
if history:
st.info("Chat History")
for i, entry in enumerate(history):
if len(entry) == 2:
st.write("Query {}: {}".format(i+1, entry[0]))
st.write("Response: ", entry[1])
else:
st.write("Invalid history entry at index", i)
st.write("")
input_pdf = st.file_uploader("Upload your PDF file ", type=['pdf'], accept_multiple_files=True)
# print("hi")
if input_pdf:
selected_file = st.selectbox("Select a CSV file", [file.name for file in input_pdf])
selected_index = [file.name for file in input_pdf].index(selected_file)
col1, col2 = st.columns([1, 1])
with col1:
st.info("PDF uploaded successfully")
text = []
for file in input_pdf:
text.append(extract_text_from_pdf(file))
st.text_area("PDF Text", text[selected_index], height=500)
with col2:
st.info("Chat with your PDF")
input_text = st.text_area("Enter your query",key="pdf_query")
if input_text is not None:
if st.button("Ask Query", key="ask_query_pdf"):
st.info("Your query: {}".format(input_text))
result = chat_with_pdf(text, input_text)
st.download_button(label="Download Result as Text", data="Query: {}\nResult: {}".format(input_text, result), file_name="pdf_result.txt", mime="text/plain")
st.success(result)
st.write("Chat History")
displayHistory()