Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import pdfplumber
|
3 |
+
import docx
|
4 |
+
import openai
|
5 |
+
import seaborn as sns
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
openai.api_key = 'sk-proj-PMkGJxtGRdaihzh15yJYT3BlbkFJ0bEWbrsZjjwV5d3XYSFc'
|
10 |
+
def load_file(file):
|
11 |
+
file_type = file.name.split('.')[-1]
|
12 |
+
if file_type == 'csv':
|
13 |
+
return pd.read_csv(file.name)
|
14 |
+
elif file_type in ['xls', 'xlsx']:
|
15 |
+
return pd.read_excel(file.name)
|
16 |
+
elif file_type == 'pdf':
|
17 |
+
return load_pdf(file)
|
18 |
+
elif file_type in ['doc', 'docx']:
|
19 |
+
return load_doc(file)
|
20 |
+
else:
|
21 |
+
raise ValueError("Unsupported file type")
|
22 |
+
|
23 |
+
def load_pdf(file):
|
24 |
+
with pdfplumber.open(file.name) as pdf:
|
25 |
+
pages = [page.extract_text() for page in pdf.pages]
|
26 |
+
text = "\n".join(pages)
|
27 |
+
return pd.DataFrame({"text": [text]})
|
28 |
+
|
29 |
+
def load_doc(file):
|
30 |
+
doc = docx.Document(file.name)
|
31 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
32 |
+
return pd.DataFrame({"text": [text]})
|
33 |
+
def generate_query(prompt):
|
34 |
+
response = openai.Completion.create(
|
35 |
+
engine="text-davinci-003",
|
36 |
+
prompt=prompt,
|
37 |
+
max_tokens=150
|
38 |
+
)
|
39 |
+
return response.choices[0].text.strip()
|
40 |
+
|
41 |
+
def handle_query(query, df):
|
42 |
+
if "number of columns" in query.lower():
|
43 |
+
return f"The number of columns is {df.shape[1]}"
|
44 |
+
elif "number of rows" in query.lower():
|
45 |
+
return f"The number of rows is {df.shape[0]}"
|
46 |
+
else:
|
47 |
+
try:
|
48 |
+
# Try executing the query as a pandas query
|
49 |
+
result_df = df.query(query)
|
50 |
+
return result_df.to_html()
|
51 |
+
except Exception as e:
|
52 |
+
return str(e)
|
53 |
+
|
54 |
+
def draw_chart(query, df):
|
55 |
+
try:
|
56 |
+
result_df = df.query(query)
|
57 |
+
sns.scatterplot(data=result_df, x=result_df.columns[0], y=result_df.columns[1])
|
58 |
+
plt.title("Generated Chart")
|
59 |
+
plt.xlabel(result_df.columns[0])
|
60 |
+
plt.ylabel(result_df.columns[1])
|
61 |
+
plt.savefig('/content/chart.png')
|
62 |
+
plt.close()
|
63 |
+
return '/content/chart.png'
|
64 |
+
except Exception as e:
|
65 |
+
return str(e)
|
66 |
+
|
67 |
+
def generate_query(prompt):
|
68 |
+
response = openai.ChatCompletion.create(
|
69 |
+
model="gpt-3.5-turbo",
|
70 |
+
messages=[
|
71 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
72 |
+
{"role": "user", "content": prompt}
|
73 |
+
]
|
74 |
+
)
|
75 |
+
return response['choices'][0]['message']['content'].strip()
|
76 |
+
|
77 |
+
def handle_query(query, df):
|
78 |
+
if "number of columns" in query.lower():
|
79 |
+
return f"The number of columns is {df.shape[1]}"
|
80 |
+
elif "number of rows" in query.lower():
|
81 |
+
return f"The number of rows is {df.shape[0]}"
|
82 |
+
else:
|
83 |
+
try:
|
84 |
+
result_df = df.query(query)
|
85 |
+
return result_df.to_html()
|
86 |
+
except Exception as e:
|
87 |
+
return str(e)
|
88 |
+
|
89 |
+
def draw_chart(query, df):
|
90 |
+
try:
|
91 |
+
result_df = df.query(query)
|
92 |
+
sns.scatterplot(data=result_df, x=result_df.columns[0], y=result_df.columns[1])
|
93 |
+
plt.title("Generated Chart")
|
94 |
+
plt.xlabel(result_df.columns[0])
|
95 |
+
plt.ylabel(result_df.columns[1])
|
96 |
+
plt.savefig('/content/chart.png')
|
97 |
+
plt.close()
|
98 |
+
return '/content/chart.png'
|
99 |
+
except Exception as e:
|
100 |
+
return str(e)
|
101 |
+
|
102 |
+
def chatbot(file, input_text):
|
103 |
+
try:
|
104 |
+
# Load the file into a DataFrame
|
105 |
+
df = load_file(file)
|
106 |
+
|
107 |
+
# Generate a query from the input text
|
108 |
+
query = generate_query(input_text)
|
109 |
+
|
110 |
+
# Handle the query and generate a response
|
111 |
+
response = handle_query(query, df)
|
112 |
+
|
113 |
+
# If the query is suitable for generating a chart, do so
|
114 |
+
if "chart" in query.lower() or "graph" in query.lower():
|
115 |
+
chart_path = draw_chart(query, df)
|
116 |
+
return chart_path, response
|
117 |
+
|
118 |
+
# Return the query response
|
119 |
+
return None, response
|
120 |
+
except Exception as e:
|
121 |
+
return None, str(e)
|
122 |
+
|
123 |
+
# Create a Gradio interface
|
124 |
+
iface = gr.Interface(
|
125 |
+
fn=chatbot,
|
126 |
+
inputs=[gr.File(type="filepath", label="Upload File"), gr.Textbox(lines=2, placeholder="Enter your query here...")],
|
127 |
+
outputs=["image", "html"],
|
128 |
+
title="Data Analyst Chatbot",
|
129 |
+
description="Upload a file and enter a query to get responses based on the data."
|
130 |
+
)
|
131 |
+
|
132 |
+
# Launch the interface
|
133 |
+
iface.launch()
|