Spaces:
Runtime error
Runtime error
File size: 1,223 Bytes
c67c1f3 f752821 c67c1f3 11dd22e c67c1f3 | 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 | 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) |