File size: 1,606 Bytes
d1d1d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d25460
 
 
 
 
 
d1d1d6a
5d25460
 
 
 
 
 
 
 
d1d1d6a
 
941c7c6
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
from langchain_community.document_loaders import PyPDFLoader
from langchain_core.messages import AIMessage, HumanMessage
from pydantic import BaseModel
import time
import gradio as gr
import requests
from typing import Generator
    
chat_history = []

def generate_response(chat_input: str, bot_message: str) -> Generator[str, str, str] | str:
    url = "http://127.0.0.1:8000/generatechat/"
    payload = {
        'question': chat_input,
    }
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.post(url, json=payload, headers=headers)
    if response.status_code == 200:
        data = response.json()
        answer = data['response']['answer']
        print("Success:", response.json())
        
        # Get a typewriting animation response
        partial_response = ""
        for char in answer:
            partial_response += char
            yield partial_response
            time.sleep(0.005)
    else:
        print("Error:", response.status_code, response.text)
        return f"Error: {response.status_code}, {response.text}"
        
CSS ="""
.contain { display: flex; flex-direction: column; }
.gradio-container { height: 100vh !important; }
#component-0 { height: 100%; }
#chatbot { flex-grow: 1; overflow: auto;}
"""

with gr.Blocks() as demo:
    chatbot = gr.Chatbot(elem_id="chatbot")
    chatbot = gr.ChatInterface(
        fn=generate_response, 
        title="AskmeAboutRAG Chat",
        description="RAG model for asking about RAG",
        chatbot=chatbot,
    )

if __name__ == "__main__":
    demo.launch(server_name = "0.0.0.0")