saicharantej commited on
Commit
c67c1f3
·
1 Parent(s): b576753

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+ from langchain.llms import OpenAI
4
+ from langchain.chains import LLMChain
5
+ from langchain.prompts import PromptTemplate
6
+ from langchain.agents import create_pandas_dataframe_agent
7
+ import os
8
+ import openai
9
+ openai.api_key = os.getenv('api_token')
10
+
11
+ # Load the CSV file
12
+ def load_csv(file):
13
+ print ("File is",file)
14
+ df = pd.read_csv(file.name)
15
+ print (df.head())
16
+ return df
17
+
18
+ # Define the function that generates the response
19
+ def generate_response(question, file):
20
+ # Load the CSV file
21
+ df = load_csv(file)
22
+
23
+ # Initialize OpenAI pipeline
24
+ agent = create_pandas_dataframe_agent(OpenAI(temperature=0,openai_api_key=openai.api_key), df, verbose=True)
25
+
26
+ # Generate response using OpenAI
27
+ response = agent.run(question)
28
+
29
+ return response
30
+
31
+ # Define the input and output interfaces
32
+ title = "DataFrame Detective: Ask questions directly to your dataframe"
33
+ csv_file = gr.inputs.File(label="CSV File")
34
+ question = gr.inputs.Textbox(label="Question")
35
+ output_text = gr.outputs.Textbox(label="Response")
36
+
37
+ # Create the Gradio app
38
+ gr.Interface(generate_response, inputs=[question, csv_file], outputs=output_text, title=title).launch(debug=True)