Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
css = """footer {visibility: hidden}
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
with gr.Blocks(theme=gr.themes.Soft(),title="data-analysist-ai",css=css) as demo:
|
| 10 |
+
chatbot = gr.Chatbot([[None,"Hi"]],avatar_images=("https://cdnl.iconscout.com/lottie/premium/thumb/user-profile-5568736-4644453.gif","https://cdn.dribbble.com/users/77598/screenshots/16399264/media/d86ceb1ad552398787fb76f343080aa6.gif"),height=500,show_label=False,show_copy_button=True,show_share_button=True,likeable=True,layout="panel")
|
| 11 |
+
with gr.Row():
|
| 12 |
+
msg = gr.Textbox(scale=3,show_label=False,placeholder="type anything and press enter")
|
| 13 |
+
upload_file = gr.UploadButton("Upload a CSV",type="filepath",scale=1, file_types=["csv"])
|
| 14 |
+
clear = gr.Button("Clear")
|
| 15 |
+
def upload_file_to_chat(file_path,chat_history):
|
| 16 |
+
chat_history.append(("<img src='https://cdn.dribbble.com/users/4625326/screenshots/19602645/media/a38d52a0a465a2265aee186316cfa590.gif'> <br><br> your file uploaded",None))
|
| 17 |
+
return chat_history
|
| 18 |
+
|
| 19 |
+
def new_chat():
|
| 20 |
+
return [[None,None]],""
|
| 21 |
+
|
| 22 |
+
def user(user_message, history):
|
| 23 |
+
return history + [[user_message, None]]
|
| 24 |
+
|
| 25 |
+
def bot(history,msg,upload_file):
|
| 26 |
+
url = 'https://research-project-h4fb.onrender.com/uploadfile/?email=a@gmail.com&query='+msg
|
| 27 |
+
file_path = upload_file
|
| 28 |
+
|
| 29 |
+
# Prepare the file in the correct format for the POST request
|
| 30 |
+
files = {'file': ('data_ISP.csv', open(file_path, 'rb'), 'text/csv')}
|
| 31 |
+
|
| 32 |
+
# Execute the POST request
|
| 33 |
+
response = requests.post(url, files=files,stream=True)
|
| 34 |
+
|
| 35 |
+
history[-1][1] = ""
|
| 36 |
+
for chunk in response:
|
| 37 |
+
processed_chunk = chunk.decode('utf-8')
|
| 38 |
+
history[-1][1] += processed_chunk
|
| 39 |
+
# streaming chunks to ui
|
| 40 |
+
yield history,""
|
| 41 |
+
|
| 42 |
+
msg.submit(user, [msg, chatbot], [chatbot], queue=False).then(
|
| 43 |
+
bot, [chatbot,msg,upload_file], [chatbot,msg]
|
| 44 |
+
)
|
| 45 |
+
upload_file.upload(upload_file_to_chat,[upload_file,chatbot], [chatbot])
|
| 46 |
+
clear.click(new_chat,outputs=[chatbot,msg]).then(lambda: None, None, chatbot, queue=False)
|
| 47 |
+
|
| 48 |
+
demo.queue()
|
| 49 |
+
demo.launch()
|