-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradio_app.py
More file actions
94 lines (76 loc) 路 3.29 KB
/
Copy pathgradio_app.py
File metadata and controls
94 lines (76 loc) 路 3.29 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import gradio as gr
import requests
import json
import os
API_URL = "http://127.0.0.1:8000/api/chat/"
def process_request(input_type, text_input, file_input):
payload = {}
if input_type == "Text Input":
if not text_input:
return {"error": "Please enter text."}, None
try:
# Let's try to parse it as JSON first.
try:
payload = json.loads(text_input)
except json.JSONDecodeError:
# If not JSON, wrap it as a single question
payload = {
"equipe": "GhostTruth",
"question": {
"Manual_Input": {
"Q1": text_input
}
}
}
except Exception as e:
return {"error": f"Error processing text input: {str(e)}"}, None
elif input_type == "File Input":
if not file_input:
return {"error": "Please upload a file."}, None
try:
# file_input is a temp file path
with open(file_input.name, 'r', encoding='utf-8') as f:
payload = json.load(f)
except Exception as e:
return {"error": f"Error processing file: {str(e)}"}, None
# Send to API
try:
response = requests.post(API_URL, json=payload)
response.raise_for_status()
result_json = response.json()
# Save to a temporary file for download
output_filename = "response.json"
with open(output_filename, "w", encoding='utf-8') as f:
json.dump(result_json, f, indent=2, ensure_ascii=False)
return result_json, output_filename
except requests.exceptions.RequestException as e:
return {"error": f"API Error: {str(e)}", "detail": f"Make sure the FastAPI server is running at {API_URL}"}, None
except Exception as e:
return {"error": str(e)}, None
with gr.Blocks(title="Heavy RAG Interface") as demo:
gr.Markdown("# Algerie Telecom RAG System")
gr.Markdown("Select input method to ask questions.")
with gr.Row():
input_type = gr.Radio(["Text Input", "File Input"], label="Input Method", value="Text Input")
with gr.Row(visible=True) as text_row:
text_input = gr.Textbox(lines=10, label="Paste JSON or Question", placeholder='{"equipe": "...", "question": {...}} or just "What is Idoom?"')
with gr.Row(visible=False) as file_row:
file_input = gr.File(label="Upload JSON File", file_types=[".json"])
# Toggle visibility based on radio selection
def toggle_input(choice):
if choice == "Text Input":
return gr.update(visible=True), gr.update(visible=False)
else:
return gr.update(visible=False), gr.update(visible=True)
input_type.change(fn=toggle_input, inputs=input_type, outputs=[text_row, file_row])
submit_btn = gr.Button("Submit", variant="primary")
with gr.Row():
result_output = gr.JSON(label="Response JSON")
download_btn = gr.File(label="Download Answer")
submit_btn.click(
fn=process_request,
inputs=[input_type, text_input, file_input],
outputs=[result_output, download_btn]
)
if __name__ == "__main__":
demo.launch(server_port=7860)