subhrajit-katonic
commited on
Commit
·
668b795
1
Parent(s):
38a4150
initial commit
Browse files- app.py +54 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import random
|
| 4 |
+
import time
|
| 5 |
+
from pandasai import PandasAI
|
| 6 |
+
from pandasai.llm.openai import OpenAI
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
df = pd.DataFrame()
|
| 10 |
+
OPENAPI_KEY_VAL = ""
|
| 11 |
+
llm = ""
|
| 12 |
+
pandas_ai = ""
|
| 13 |
+
|
| 14 |
+
def process_file(file, OPENAPI_KEY):
|
| 15 |
+
global df, OPENAPI_KEY_VAL, llm, pandas_ai
|
| 16 |
+
df = pd.read_csv(file.name)
|
| 17 |
+
OPENAPI_KEY_VAL = OPENAPI_KEY
|
| 18 |
+
print(OPENAPI_KEY_VAL)
|
| 19 |
+
|
| 20 |
+
llm = OpenAI(api_token = OPENAPI_KEY_VAL)
|
| 21 |
+
pandas_ai = PandasAI(llm)
|
| 22 |
+
|
| 23 |
+
return df.head(2)
|
| 24 |
+
|
| 25 |
+
with gr.Blocks() as demo:
|
| 26 |
+
|
| 27 |
+
with gr.Row():
|
| 28 |
+
file = gr.inputs.File(type="file", label="Upload CSV")
|
| 29 |
+
key = gr.Textbox(placeholder="Paste OPENAI API KEY")
|
| 30 |
+
out = gr.DataFrame()
|
| 31 |
+
|
| 32 |
+
btn = gr.Button("Execute")
|
| 33 |
+
btn.click(fn=process_file, inputs=[file, key], outputs=out)
|
| 34 |
+
|
| 35 |
+
chatbot = gr.Chatbot()
|
| 36 |
+
msg = gr.Textbox()
|
| 37 |
+
clear = gr.Button("Clear")
|
| 38 |
+
|
| 39 |
+
def respond(message, chat_history):
|
| 40 |
+
bot_message = "Hi"
|
| 41 |
+
try:
|
| 42 |
+
bot_message = pandas_ai.run(df, prompt=message)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(e)
|
| 45 |
+
bot_message = "I'm sorry, but as an AI language model, I am unable to provide a meaningful response to this question."
|
| 46 |
+
# bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
| 47 |
+
chat_history.append((message, bot_message))
|
| 48 |
+
time.sleep(1)
|
| 49 |
+
return "", chat_history
|
| 50 |
+
|
| 51 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 52 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 53 |
+
|
| 54 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.18.0
|
| 2 |
+
pandasai==0.2.13
|