File size: 2,153 Bytes
e539905
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
import gradio as gr
import random
import time

import requests
css = """footer {visibility: hidden}
"""

with gr.Blocks(theme=gr.themes.Soft(),title="data-analysist-ai",css=css) as demo:
    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")
    with gr.Row():
        msg = gr.Textbox(scale=3,show_label=False,placeholder="type anything and press enter")
        upload_file = gr.UploadButton("Upload a CSV",type="filepath",scale=1, file_types=["csv"])
    clear = gr.Button("Clear")
    def upload_file_to_chat(file_path,chat_history):
            chat_history.append(("<img src='https://cdn.dribbble.com/users/4625326/screenshots/19602645/media/a38d52a0a465a2265aee186316cfa590.gif'> <br><br> your file uploaded",None))
            return chat_history

    def new_chat():
        return [[None,None]],""

    def user(user_message, history):
        return history + [[user_message, None]]

    def bot(history,msg,upload_file):
        url = 'https://research-project-h4fb.onrender.com/uploadfile/?email=a@gmail.com&query='+msg
        file_path = upload_file

        # Prepare the file in the correct format for the POST request
        files = {'file': ('data_ISP.csv', open(file_path, 'rb'), 'text/csv')}

        # Execute the POST request
        response = requests.post(url, files=files,stream=True)

        history[-1][1] = ""
        for chunk in response:
            processed_chunk = chunk.decode('utf-8')
            history[-1][1] += processed_chunk
            # streaming chunks to ui
            yield history,""

    msg.submit(user, [msg, chatbot], [chatbot], queue=False).then(
        bot, [chatbot,msg,upload_file], [chatbot,msg]
    )
    upload_file.upload(upload_file_to_chat,[upload_file,chatbot], [chatbot])
    clear.click(new_chat,outputs=[chatbot,msg]).then(lambda: None, None, chatbot, queue=False)

demo.queue()
demo.launch()