Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# import httpx
|
3 |
+
# import json
|
4 |
+
# from typing import Tuple, Any
|
5 |
+
|
6 |
+
# # Define the FastAPI endpoint URL
|
7 |
+
# FASTAPI_ENDPOINT = "http://localhost:8000/websearch"
|
8 |
+
|
9 |
+
|
10 |
+
# def query_api(query: str) -> Tuple[Any, Any]:
|
11 |
+
# try:
|
12 |
+
# # Send POST request to FastAPI endpoint with streaming enabled
|
13 |
+
# with httpx.Client() as client:
|
14 |
+
# with client.stream("POST", FASTAPI_ENDPOINT, json={"query": query}, timeout=60.0) as response:
|
15 |
+
# response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
|
16 |
+
|
17 |
+
# # Process the streaming response
|
18 |
+
# response_data = ""
|
19 |
+
# for chunk in response.iter_text():
|
20 |
+
# response_data += chunk
|
21 |
+
|
22 |
+
# # Parse the accumulated response data as JSON
|
23 |
+
# response_json = json.loads(response_data)
|
24 |
+
|
25 |
+
# # Extract content and citations from the response JSON
|
26 |
+
# content = response_json.get("content", "")
|
27 |
+
# citations = response_json.get("citations", [])
|
28 |
+
|
29 |
+
# # Beautify content using Markdown formatting
|
30 |
+
# beautified_content = f"# Search Results\n\n{content}"
|
31 |
+
|
32 |
+
# # Beautify citations by adding Markdown links
|
33 |
+
# beautified_citations = "# Citations\n\n"
|
34 |
+
# for i, citation in enumerate(citations, start=1):
|
35 |
+
# beautified_citations += f"{i}. [{citation}]({citation})\n"
|
36 |
+
|
37 |
+
# # Yield the beautified content and citations
|
38 |
+
# yield beautified_content, beautified_citations
|
39 |
+
# except httpx.TimeoutException:
|
40 |
+
# yield "Request timed out. Please try again later.", ""
|
41 |
+
# except httpx.HTTPStatusError as e:
|
42 |
+
# yield f"HTTP error occurred: {e}", ""
|
43 |
+
# except Exception as e:
|
44 |
+
# yield f"An error occurred: {e}", ""
|
45 |
+
|
46 |
+
|
47 |
+
# # Create Gradio interface
|
48 |
+
# with gr.Blocks(css=".gradio-container { background-color: #f5f5f5; padding: 20px; border-radius: 10px; }") as demo:
|
49 |
+
# gr.Markdown("# Web Search Application")
|
50 |
+
|
51 |
+
# with gr.Row():
|
52 |
+
# with gr.Column():
|
53 |
+
# query = gr.Textbox(
|
54 |
+
# label="Enter your query",
|
55 |
+
# placeholder="Type your search query here...",
|
56 |
+
# lines=2,
|
57 |
+
# max_lines=4,
|
58 |
+
# value="",
|
59 |
+
# elem_id="query-input"
|
60 |
+
# )
|
61 |
+
# submit_button = gr.Button("Search")
|
62 |
+
|
63 |
+
# with gr.Column():
|
64 |
+
# output_content = gr.Textbox(
|
65 |
+
# label="Response Content",
|
66 |
+
# placeholder="Search results will appear here...",
|
67 |
+
# lines=10,
|
68 |
+
# max_lines=20,
|
69 |
+
# value="",
|
70 |
+
# elem_id="response-content"
|
71 |
+
# )
|
72 |
+
# output_citations = gr.Textbox(
|
73 |
+
# label="Citations",
|
74 |
+
# placeholder="Citations will appear here...",
|
75 |
+
# lines=5,
|
76 |
+
# max_lines=10,
|
77 |
+
# value="",
|
78 |
+
# elem_id="response-citations"
|
79 |
+
# )
|
80 |
+
|
81 |
+
# # Set up event listener
|
82 |
+
# submit_button.click(query_api, inputs=query, outputs=[output_content, output_citations])
|
83 |
+
|
84 |
+
# gr.Markdown("Powered by FastAPI and Gradio")
|
85 |
+
|
86 |
+
# # Launch the Gradio application
|
87 |
+
# demo.launch()
|
88 |
+
|
89 |
+
|
90 |
+
import gradio as gr
|
91 |
+
import httpx
|
92 |
+
import json
|
93 |
+
|
94 |
+
# Define the FastAPI endpoint URL
|
95 |
+
FASTAPI_ENDPOINT = "http://localhost:8000/websearch"
|
96 |
+
|
97 |
+
def query_api(query: str) -> tuple:
|
98 |
+
try:
|
99 |
+
# Send POST request to FastAPI endpoint with streaming enabled
|
100 |
+
with httpx.Client() as client:
|
101 |
+
with client.stream("POST", FASTAPI_ENDPOINT, json={"query": query}, timeout=60.0) as response:
|
102 |
+
response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
|
103 |
+
|
104 |
+
# Process the streaming response
|
105 |
+
response_data = ""
|
106 |
+
for chunk in response.iter_text():
|
107 |
+
response_data += chunk
|
108 |
+
|
109 |
+
# Parse the accumulated response data as JSON
|
110 |
+
response_json = json.loads(response_data)
|
111 |
+
|
112 |
+
# Extract content and citations from the response JSON
|
113 |
+
content = response_json.get("content", "")
|
114 |
+
citations = response_json.get("citations", [])
|
115 |
+
|
116 |
+
# Beautify content using Markdown formatting
|
117 |
+
beautified_content = f"# Search Results\n\n{content}"
|
118 |
+
|
119 |
+
# Beautify citations by adding Markdown links
|
120 |
+
beautified_citations = "# Citations/Sources\n\n"
|
121 |
+
for i, citation in enumerate(citations, start=1):
|
122 |
+
beautified_citations += f"{i}. [{citation}]({citation})\n"
|
123 |
+
|
124 |
+
# Yield the beautified content and citations
|
125 |
+
yield beautified_content, beautified_citations
|
126 |
+
except httpx.TimeoutException:
|
127 |
+
yield "# Request Timeout\n\nRequest timed out. Please try again later.", ""
|
128 |
+
except httpx.HTTPStatusError as e:
|
129 |
+
yield f"# HTTP Error\n\nHTTP error occurred: {e}", ""
|
130 |
+
except Exception as e:
|
131 |
+
yield f"# Error\n\nAn error occurred: {e}", ""
|
132 |
+
|
133 |
+
# Create Gradio interface
|
134 |
+
with gr.Blocks(css=".gradio-container { background-color: #f5f5f5; padding: 20px; border-radius: 10px; }", theme=gr.themes.Citrus()) as demo:
|
135 |
+
gr.Markdown("# Web Search Application")
|
136 |
+
|
137 |
+
with gr.Row():
|
138 |
+
with gr.Column(
|
139 |
+
render=True,
|
140 |
+
show_progress=True
|
141 |
+
):
|
142 |
+
query = gr.Textbox(
|
143 |
+
label="Enter your query",
|
144 |
+
placeholder="Type your search query here...",
|
145 |
+
lines=2,
|
146 |
+
max_lines=4,
|
147 |
+
value="",
|
148 |
+
elem_id="query-input"
|
149 |
+
)
|
150 |
+
submit_button = gr.Button("Search")
|
151 |
+
|
152 |
+
with gr.Column(
|
153 |
+
render=True,
|
154 |
+
show_progress=True
|
155 |
+
):
|
156 |
+
output_content = gr.Markdown(
|
157 |
+
label="Response Content",
|
158 |
+
value="",
|
159 |
+
elem_id="response-content",
|
160 |
+
height="600px",
|
161 |
+
visible=True,
|
162 |
+
show_label=True
|
163 |
+
|
164 |
+
)
|
165 |
+
output_citations = gr.Markdown(
|
166 |
+
label="Citations",
|
167 |
+
value="",
|
168 |
+
elem_id="response-citations",
|
169 |
+
height="200px",
|
170 |
+
visible=True,
|
171 |
+
show_label=True
|
172 |
+
)
|
173 |
+
|
174 |
+
# Set up event listener
|
175 |
+
submit_button.click(query_api, inputs=query, outputs=[output_content, output_citations])
|
176 |
+
|
177 |
+
gr.Markdown("Powered by FastAPI and Gradio")
|
178 |
+
|
179 |
+
# Launch the Gradio application
|
180 |
+
demo.launch()
|