|
import gradio as gr |
|
from langchain.llms import OpenAI |
|
from langchain.chat_models import ChatOpenAI |
|
from langchain.agents.agent_types import AgentType |
|
from langchain_experimental.agents.agent_toolkits import create_csv_agent |
|
import os |
|
|
|
spaces_key = os.getenv("open_ai") |
|
|
|
def call_csv(file_path): |
|
|
|
agent = create_csv_agent( |
|
ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k", openai_api_key=spaces_key), |
|
file_path, |
|
verbose=True, |
|
agent_type=AgentType.OPENAI_FUNCTIONS,) |
|
return agent |
|
|
|
|
|
def qa_app(csv_file, question): |
|
|
|
agent = call_csv(csv_file) |
|
|
|
|
|
response = agent.run(question) |
|
|
|
return response |
|
|
|
|
|
iface = gr.Interface( |
|
fn=qa_app, |
|
inputs=["file", "text"], |
|
outputs="text", |
|
live=False, |
|
title="CSV QA App", |
|
description="Upload a CSV file and ask a question about the data.", |
|
) |
|
iface.launch() |