Spaces:
openfree
/
Running on CPU Upgrade

seawolf2357 commited on
Commit
f2842d4
ยท
verified ยท
1 Parent(s): 8ebd751

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+
5
+ API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
6
+
7
+ MAJOR_COUNTRIES = [
8
+ "United States", "United Kingdom", "Canada", "Australia", "Germany",
9
+ "France", "Japan", "South Korea", "China", "India",
10
+ "Brazil", "Mexico", "Russia", "Italy", "Spain",
11
+ "Netherlands", "Sweden", "Switzerland", "Norway", "Denmark",
12
+ "Finland", "Belgium", "Austria", "New Zealand", "Ireland",
13
+ "Singapore", "Hong Kong", "Israel", "United Arab Emirates", "Saudi Arabia",
14
+ "South Africa", "Turkey", "Egypt", "Poland", "Czech Republic",
15
+ "Hungary", "Greece", "Portugal", "Argentina", "Chile",
16
+ "Colombia", "Peru", "Venezuela", "Thailand", "Malaysia",
17
+ "Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh"
18
+ ]
19
+
20
+ def search_serphouse(query, country, page, num_result):
21
+ url = "https://api.serphouse.com/serp/live"
22
+ payload = {
23
+ "data": {
24
+ "q": query,
25
+ "domain": "google.com",
26
+ "loc": country,
27
+ "lang": "en",
28
+ "device": "desktop",
29
+ "serp_type": "news",
30
+ "page": str(page),
31
+ "verbatim": "1",
32
+ "num": str(num_result)
33
+ }
34
+ }
35
+
36
+ headers = {
37
+ "accept": "application/json",
38
+ "content-type": "application/json",
39
+ "authorization": f"Bearer {API_KEY}"
40
+ }
41
+
42
+ try:
43
+ response = requests.post(url, json=payload, headers=headers)
44
+ response.raise_for_status()
45
+ return response.json()
46
+ except requests.RequestException as e:
47
+ return f"Error: {str(e)}"
48
+
49
+ def format_results(results):
50
+ all_results = "## ๋ชจ๋“  ๋‰ด์Šค ๊ฒฐ๊ณผ\n\n"
51
+ result_table = "## ๋‰ด์Šค ๊ฒฐ๊ณผ ํ‘œ\n\n"
52
+ result_table += "| ์ œ๋ชฉ | ๋งํฌ | ์‹œ๊ฐ„ | ์ถœ์ฒ˜ | ๋””๋ฒ„๊ทธ ์ •๋ณด |\n"
53
+ result_table += "|------|------|------|------|-------------|\n"
54
+
55
+ debug_info = "## ๋””๋ฒ„๊ทธ ์ •๋ณด\n\n"
56
+ debug_info += f"Raw API Response:\n```json\n{json.dumps(results, indent=2)}\n```\n\n"
57
+
58
+ try:
59
+ if not isinstance(results, dict):
60
+ raise ValueError("๊ฒฐ๊ณผ๊ฐ€ ์‚ฌ์ „ ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค.")
61
+
62
+ if "results" not in results:
63
+ raise ValueError("'results' ํ‚ค๊ฐ€ ์‘๋‹ต์— ์—†์Šต๋‹ˆ๋‹ค.")
64
+
65
+ news_results = results["results"].get("news", [])
66
+ debug_info += f"๋‰ด์Šค ๊ฒฐ๊ณผ ์ˆ˜: {len(news_results)}\n\n"
67
+
68
+ for idx, result in enumerate(news_results):
69
+ title = result.get("title", "์ œ๋ชฉ ์—†์Œ")
70
+ url = result.get("url", "#")
71
+ snippet = result.get("snippet", "๋‚ด์šฉ ์—†์Œ")
72
+ channel = result.get("channel", "์•Œ ์ˆ˜ ์—†์Œ")
73
+ time_str = result.get("time", "์•Œ ์ˆ˜ ์—†๋Š” ์‹œ๊ฐ„")
74
+
75
+ single_debug_info = f"๋‰ด์Šค {idx + 1} - ์ œ๋ชฉ: {title}, ๋งํฌ: {url}, ์‹œ๊ฐ„: {time_str}, ์ถœ์ฒ˜: {channel}"
76
+
77
+ result_table += f"| {title[:30]}... | [{url[:30]}...]({url}) | {time_str} | {channel} | {single_debug_info[:50]}... |\n"
78
+
79
+ article_info = f"""
80
+ ### [{title}]({url})
81
+ {snippet}
82
+ **์ถœ์ฒ˜:** {channel} - {time_str}
83
+ ---
84
+ """
85
+ all_results += article_info
86
+
87
+ except Exception as e:
88
+ error_message = f"๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
89
+ debug_info += error_message + "\n"
90
+ all_results = error_message + "\n\n"
91
+
92
+ result_table += f"| ์˜ค๋ฅ˜ ๋ฐœ์ƒ | - | - | - | {error_message} |\n"
93
+
94
+ return all_results, result_table + "\n\n" + debug_info
95
+
96
+
97
+ def serphouse_search(query, country, page, num_result):
98
+ results = search_serphouse(query, country, page, num_result)
99
+ all_results, result_table_and_debug = format_results(results)
100
+ return all_results, result_table_and_debug
101
+
102
+ css = """
103
+ footer {
104
+ visibility: hidden;
105
+ }
106
+ """
107
+
108
+ iface = gr.Interface(
109
+ fn=serphouse_search,
110
+ inputs=[
111
+ gr.Textbox(label="๊ฒ€์ƒ‰์–ด"),
112
+ gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ€"),
113
+ gr.Slider(1, 10, 1, label="ํŽ˜์ด์ง€"),
114
+ gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์ˆ˜")
115
+ ],
116
+ outputs=[
117
+ gr.Markdown(label="๋ชจ๋“  ๊ฒฐ๊ณผ"),
118
+ gr.Markdown(label="๊ฒฐ๊ณผ ํ‘œ ๋ฐ ๋””๋ฒ„๊ทธ ์ •๋ณด")
119
+ ],
120
+ title="SERPHouse ๋‰ด์Šค ๊ฒ€์ƒ‰ ์ธํ„ฐํŽ˜์ด์Šค",
121
+ description="๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๊ตญ๊ฐ€๋ฅผ ์„ ํƒํ•˜์—ฌ SERPHouse API์—์„œ ๋‰ด์Šค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.",
122
+ theme="Nymbo/Nymbo_Theme",
123
+ css=css
124
+ )
125
+
126
+ iface.launch()