Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import pandas as pd
|
5 |
+
from typing import Tuple
|
6 |
+
|
7 |
+
|
8 |
+
def search_stories(query: str, page: int) -> Tuple[pd.DataFrame, int]:
|
9 |
+
"""
|
10 |
+
Search stories from local API and return results as DataFrame
|
11 |
+
"""
|
12 |
+
try:
|
13 |
+
response = requests.post(
|
14 |
+
url=os.environ.get("API_URL", "http://50.18.255.74:8600/search"),
|
15 |
+
json={"query": query, "page": page},
|
16 |
+
headers={"Content-Type": "application/json"},
|
17 |
+
)
|
18 |
+
response.raise_for_status()
|
19 |
+
|
20 |
+
# Convert response data to DataFrame
|
21 |
+
data = response.json()["hits"]
|
22 |
+
df = pd.DataFrame(data)
|
23 |
+
|
24 |
+
# Reorder columns for better display
|
25 |
+
columns = ["title", "author", "story_text", "created_at", "points"]
|
26 |
+
df = df[columns]
|
27 |
+
|
28 |
+
return df, page
|
29 |
+
except requests.RequestException as e:
|
30 |
+
print(e)
|
31 |
+
return pd.DataFrame(), page
|
32 |
+
|
33 |
+
|
34 |
+
def next_page(query: str, current_page: int) -> Tuple[pd.DataFrame, int]:
|
35 |
+
"""
|
36 |
+
Load next page of results
|
37 |
+
"""
|
38 |
+
next_page = current_page + 1
|
39 |
+
return search_stories(query, next_page)
|
40 |
+
|
41 |
+
|
42 |
+
# Create Gradio interface
|
43 |
+
with gr.Blocks() as app:
|
44 |
+
gr.Markdown("# Story Search")
|
45 |
+
|
46 |
+
# Input components
|
47 |
+
with gr.Row():
|
48 |
+
query_input = gr.Textbox(
|
49 |
+
label="Search Query", placeholder="Enter search terms..."
|
50 |
+
)
|
51 |
+
page_state = gr.State(value=0)
|
52 |
+
|
53 |
+
# Search button
|
54 |
+
search_btn = gr.Button("Search")
|
55 |
+
|
56 |
+
# Results display
|
57 |
+
results_df = gr.DataFrame(label="Search Results", interactive=False, wrap=True)
|
58 |
+
|
59 |
+
# Next page button
|
60 |
+
next_btn = gr.Button("Next Page")
|
61 |
+
|
62 |
+
# Handle search button click
|
63 |
+
search_btn.click(
|
64 |
+
fn=search_stories,
|
65 |
+
inputs=[query_input, page_state],
|
66 |
+
outputs=[results_df, page_state],
|
67 |
+
)
|
68 |
+
|
69 |
+
# Handle next page button click
|
70 |
+
next_btn.click(
|
71 |
+
fn=next_page, inputs=[query_input, page_state], outputs=[results_df, page_state]
|
72 |
+
)
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
app.launch()
|