Tamqeen commited on
Commit
4213c4a
1 Parent(s): 8d76f89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -39
app.py CHANGED
@@ -6,7 +6,9 @@ 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':
@@ -30,41 +32,8 @@ 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=[
@@ -99,7 +68,7 @@ def draw_chart(query, df):
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)
@@ -120,15 +89,14 @@ def draw_chart(query, df):
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
- # Launch the interface
134
- iface.launch(share=True, debug=True)
 
6
  import matplotlib.pyplot as plt
7
  import gradio as gr
8
 
9
+ # Set your OpenAI API key
10
  openai.api_key = 'sk-proj-PMkGJxtGRdaihzh15yJYT3BlbkFJ0bEWbrsZjjwV5d3XYSFc'
11
+
12
  def load_file(file):
13
  file_type = file.name.split('.')[-1]
14
  if file_type == 'csv':
 
32
  doc = docx.Document(file.name)
33
  text = "\n".join([para.text for para in doc.paragraphs])
34
  return pd.DataFrame({"text": [text]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ def generate_query(prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  response = openai.ChatCompletion.create(
38
  model="gpt-3.5-turbo",
39
  messages=[
 
68
  except Exception as e:
69
  return str(e)
70
 
71
+ def chatbot(file, input_text):
72
  try:
73
  # Load the file into a DataFrame
74
  df = load_file(file)
 
89
  except Exception as e:
90
  return None, str(e)
91
 
92
+ # Create a Gradio interface
93
  iface = gr.Interface(
94
  fn=chatbot,
95
+ inputs=[gr.File(type="file", label="Upload File"), gr.Textbox(lines=2, placeholder="Enter your query here...")],
96
  outputs=["image", "html"],
97
  title="Data Analyst Chatbot",
98
  description="Upload a file and enter a query to get responses based on the data."
99
  )
100
 
101
  # Launch the interface
102
+ iface.launch()