File size: 1,731 Bytes
b7b6141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
import gradio as gr
import requests

BACKEND_URL = "https://document-qa-lmr9.onrender.com"

def upload_document(file, url):
    if file:
        with open(file.name, "rb") as f:
            response = requests.post(f"{BACKEND_URL}/upload", files = {"file": f})
    elif url:
        response = requests.post(f"{BACKEND_URL}/upload", data = {"url": url})
    else:
        return None, "Provide a file or URL"
    
    session_id = response.json()["session_id"]
    return session_id, "Document uploaded successfully!"

def chat(question, session_id, history):
    if not session_id:
        return history, "Please upload a document first."
    
    history.append({"role": "user", "content": question})
    history.append({"role": "assistant", "content": ""})
    
    with requests.post(
        f"{BACKEND_URL}/chat",
        data={"session_id": session_id, "question": question},
        stream=True
    ) as response:
        for token in response.iter_content(chunk_size=None, decode_unicode=True):
            history[-1]["content"] += token
            yield history, ""

with gr.Blocks() as demo:
    session_id = gr.State(None)

    gr.Markdown('# Document Q&A')

    with gr.Row():
        file_input = gr.File(label = "Upload PDF")
        url_input = gr.Textbox(label = "Or paste a URL")

    upload_btn = gr.Button("Upload")
    upload_status = gr.Textbox(label = "Status", interactive = False)

    chatbot = gr.Chatbot()
    question_input = gr.Textbox(label = "Ask a question")

    upload_btn.click(upload_document, inputs=[file_input, url_input], outputs=[session_id, upload_status])
    question_input.submit(chat, inputs=[question_input, session_id, chatbot], outputs=[chatbot, question_input])

demo.launch()