Spaces:
Sleeping
Sleeping
import gradio as gr | |
from build import updateBrain | |
from ask import handleSubmit | |
import json | |
from langchain.llms import OpenAI | |
from langchain.agents import create_pandas_dataframe_agent | |
import pandas as pd | |
def handleBuild(files, data): | |
brainName = json.loads(data)[0] | |
if (files == None): | |
return "Please Upload Files!" | |
build = updateBrain(brainName, files) | |
return handleAsk(data) | |
def handleAsk(braindata): | |
# try: | |
data = json.loads(braindata) | |
print(data) | |
return handleSubmit(data[0], data[1], data[2], data[3], data[4], data[5]) | |
# except: | |
# res = {"data": ["There's an error. Please try again.",{"memory": []}]} | |
# return json.dumps(res) | |
def handleCsv(file, question): | |
df = pd.read_csv(file.name) | |
for col in df.columns: | |
try: | |
if isinstance(df[col][0], str) and ('$' in df[col][0] or ',' in df[col][0]): | |
df[col] = pd.to_numeric( | |
df[col].str.replace(',', '').str.replace('$', '')) | |
except: | |
pass | |
print(df.head()) | |
agent = create_pandas_dataframe_agent( | |
OpenAI(temperature=0), df, verbose=True) | |
res = agent.run(question) | |
return res | |
with gr.Blocks() as block_demo: | |
with gr.Row(elem_id="secondrow"): | |
with gr.Column(scale=1, elem_id="inputsCol") as myrow: | |
files = gr.File(label="Upload Files", file_count="multiple", | |
file_types=[".docx", ".pdf", ".csv", ".txt", ".xml", ".json", ".mp3"], elem_id="file", visible=False) | |
build_button = gr.Button( | |
value="Submit", elem_id="button", visible=False) | |
ask_button = gr.Button( | |
value="ttt", elem_id="button", visible=False) | |
br = gr.Textbox(visible=False) | |
res = gr.TextArea(visible=False) | |
csv = gr.File(label="Upload Files", | |
file_types=[".csv"], elem_id="file", visible=False) | |
csvButton = gr.Button(value="ttt", elem_id="button", visible=False) | |
# | |
build_button.click( | |
handleBuild, [files, br], res, api_name="build") | |
ask_button.click( | |
handleAsk, [br], res, api_name="ask") | |
csvButton.click( | |
handleCsv, [csv, br], res, api_name="askcsv") | |
block_demo.queue(concurrency_count=6) | |
block_demo.launch() | |