-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.py
54 lines (43 loc) · 1.39 KB
/
frontend.py
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
54
"""
Web application to interact with the RAG system.
Author
------
Nicolas Rojas
"""
import requests
import gradio as gr
API_URL = "http://localhost:8000/query/"
def query_model(text: str) -> str:
"""Query the RAG system through its API.
Parameters
----------
text : str
Question or query made by the user.
Returns
-------
str
Response given by the API as a single string, or error logs when found.
"""
try:
# Send a POST request to the API
response = requests.post(API_URL, json={"query": text}, timeout=30)
# Check whether the request was successful
if response.status_code != 200:
return f"Error: API returned status code {response.status_code}"
result = response.json()["response"]
result += "\nThis information was obtained from the following files:\n"
for source in response.json()["source_files"]:
result += f"- {source}\n"
return result
except requests.RequestException as e:
return f"Error: Could not connect to the API. {str(e)}"
# Create the Gradio interface
interface = gr.Interface(
fn=query_model,
inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
outputs="text",
title="Docuchat",
description="Enter your question, and I'll provide with an answer from the documentation.",
)
# Launch the app
interface.launch()