Spaces:
Runtime error
Runtime error
| import pandas as pd | |
| import gradio as gr | |
| from langchain.llms import OpenAI | |
| from langchain.chains import LLMChain | |
| from langchain.prompts import PromptTemplate | |
| from langchain_experimental.agents.create_pandas_dataframe_agent import create_pandas_dataframe_agent | |
| import os | |
| import openai | |
| openai.api_key = os.getenv('api_token') | |
| # Load the CSV file | |
| def load_csv(file): | |
| print ("File is",file) | |
| df = pd.read_csv(file.name) | |
| print (df.head()) | |
| return df | |
| # Define the function that generates the response | |
| def generate_response(question, file): | |
| # Load the CSV file | |
| df = load_csv(file) | |
| # Initialize OpenAI pipeline | |
| agent = create_pandas_dataframe_agent(OpenAI(temperature=0,openai_api_key=openai.api_key), df, verbose=True) | |
| # Generate response using OpenAI | |
| response = agent.run(question) | |
| return response | |
| # Define the input and output interfaces | |
| title = "Data Detective: Ask questions directly to your data" | |
| csv_file = gr.inputs.File(label="CSV File") | |
| question = gr.inputs.Textbox(label="Question") | |
| output_text = gr.outputs.Textbox(label="Response") | |
| # Create the Gradio app | |
| gr.Interface(generate_response, inputs=[question, csv_file], outputs=output_text, title=title).launch(debug=True) |