Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -588,6 +588,8 @@ def create_main_interface():
|
|
| 588 |
|
| 589 |
async def execute_search_and_generate(query, setting):
|
| 590 |
try:
|
|
|
|
|
|
|
| 591 |
# ๊ฒ์ ์คํ
|
| 592 |
url = "https://api.serphouse.com/serp/live"
|
| 593 |
payload = {
|
|
@@ -596,70 +598,87 @@ def create_main_interface():
|
|
| 596 |
"domain": "google.com",
|
| 597 |
"lang": "en",
|
| 598 |
"device": "desktop",
|
| 599 |
-
"serp_type": "news",
|
|
|
|
| 600 |
"page": "1",
|
| 601 |
-
"num": "10"
|
|
|
|
|
|
|
| 602 |
}
|
| 603 |
}
|
| 604 |
headers = {
|
| 605 |
-
"
|
| 606 |
-
"
|
| 607 |
-
"authorization": "Bearer V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
|
| 608 |
}
|
| 609 |
|
|
|
|
| 610 |
response = requests.post(url, headers=headers, json=payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
results = response.json()
|
|
|
|
| 612 |
|
| 613 |
# ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ HTML๋ก ๋ณํํ๊ณ ์์ฝ
|
| 614 |
search_summary = "<div class='search-results'>\n"
|
| 615 |
search_summary += "<h2>์ต์ ๋ด์ค ๊ฒ์ ๊ฒฐ๊ณผ</h2>\n"
|
| 616 |
|
| 617 |
-
if 'results' in results
|
| 618 |
-
|
|
|
|
|
|
|
|
|
|
| 619 |
search_summary += f"""
|
| 620 |
<div class="search-item">
|
| 621 |
-
<h3><a href="{item
|
| 622 |
-
<p>{item
|
| 623 |
<div class="search-meta">
|
| 624 |
-
<span class="source">{item
|
| 625 |
-
<span class="time">{item
|
| 626 |
</div>
|
| 627 |
</div>
|
| 628 |
"""
|
| 629 |
search_summary += "</div>"
|
| 630 |
|
| 631 |
# ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ํฌํจํ ํฅ์๋ ํ๋กฌํํธ ์์ฑ
|
| 632 |
-
enhanced_prompt = f"""Based on
|
| 633 |
|
| 634 |
-
|
| 635 |
|
| 636 |
-
Search Results Summary:
|
| 637 |
{search_summary}
|
| 638 |
|
| 639 |
-
Please create a response that:
|
| 640 |
-
1. Summarizes the key
|
| 641 |
-
2.
|
| 642 |
-
3. Uses appropriate
|
| 643 |
-
4. Includes
|
| 644 |
-
5.
|
| 645 |
|
| 646 |
-
|
| 647 |
"""
|
| 648 |
|
|
|
|
|
|
|
| 649 |
# async generator๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์์
|
| 650 |
async for result in demo_instance.generation_code(enhanced_prompt, setting):
|
| 651 |
final_result = result
|
|
|
|
|
|
|
| 652 |
return final_result
|
| 653 |
|
| 654 |
except Exception as e:
|
| 655 |
-
print(f"Search error: {str(e)}")
|
|
|
|
| 656 |
return [
|
| 657 |
"",
|
| 658 |
None,
|
| 659 |
gr.update(active_key="error"),
|
| 660 |
gr.update(open=False)
|
| 661 |
]
|
| 662 |
-
|
| 663 |
def execute_code(query: str):
|
| 664 |
if not query or query.strip() == '':
|
| 665 |
return None, gr.update(active_key="empty")
|
|
|
|
| 588 |
|
| 589 |
async def execute_search_and_generate(query, setting):
|
| 590 |
try:
|
| 591 |
+
print(f"Executing search for query: {query}") # ๋๋ฒ๊น
์ฉ
|
| 592 |
+
|
| 593 |
# ๊ฒ์ ์คํ
|
| 594 |
url = "https://api.serphouse.com/serp/live"
|
| 595 |
payload = {
|
|
|
|
| 598 |
"domain": "google.com",
|
| 599 |
"lang": "en",
|
| 600 |
"device": "desktop",
|
| 601 |
+
"serp_type": "news",
|
| 602 |
+
"loc": "United States",
|
| 603 |
"page": "1",
|
| 604 |
+
"num": "10",
|
| 605 |
+
"ie": "UTF-8",
|
| 606 |
+
"tbm": "nws"
|
| 607 |
}
|
| 608 |
}
|
| 609 |
headers = {
|
| 610 |
+
"Authorization": "Bearer V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6",
|
| 611 |
+
"Content-Type": "application/json"
|
|
|
|
| 612 |
}
|
| 613 |
|
| 614 |
+
print("Sending API request...") # ๋๋ฒ๊น
์ฉ
|
| 615 |
response = requests.post(url, headers=headers, json=payload)
|
| 616 |
+
print(f"API Response status: {response.status_code}") # ๋๋ฒ๊น
์ฉ
|
| 617 |
+
|
| 618 |
+
if response.status_code != 200:
|
| 619 |
+
print(f"API Error: {response.text}") # ๋๋ฒ๊น
์ฉ
|
| 620 |
+
raise Exception(f"API request failed with status {response.status_code}")
|
| 621 |
+
|
| 622 |
results = response.json()
|
| 623 |
+
print(f"Got search results: {results.keys()}") # ๋๋ฒ๊น
์ฉ
|
| 624 |
|
| 625 |
# ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ HTML๋ก ๋ณํํ๊ณ ์์ฝ
|
| 626 |
search_summary = "<div class='search-results'>\n"
|
| 627 |
search_summary += "<h2>์ต์ ๋ด์ค ๊ฒ์ ๊ฒฐ๊ณผ</h2>\n"
|
| 628 |
|
| 629 |
+
if 'results' in results:
|
| 630 |
+
news_results = results['results'].get('news', [])
|
| 631 |
+
print(f"Found {len(news_results)} news items") # ๋๋ฒ๊น
์ฉ
|
| 632 |
+
|
| 633 |
+
for item in news_results[:5]: # ์์ 5๊ฐ ๊ฒฐ๊ณผ๋ง ์ฌ์ฉ
|
| 634 |
search_summary += f"""
|
| 635 |
<div class="search-item">
|
| 636 |
+
<h3><a href="{item.get('url', '#')}">{item.get('title', 'No Title')}</a></h3>
|
| 637 |
+
<p>{item.get('snippet', 'No description available')}</p>
|
| 638 |
<div class="search-meta">
|
| 639 |
+
<span class="source">{item.get('channel', 'Unknown')}</span>
|
| 640 |
+
<span class="time">{item.get('time', '')}</span>
|
| 641 |
</div>
|
| 642 |
</div>
|
| 643 |
"""
|
| 644 |
search_summary += "</div>"
|
| 645 |
|
| 646 |
# ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ํฌํจํ ํฅ์๋ ํ๋กฌํํธ ์์ฑ
|
| 647 |
+
enhanced_prompt = f"""Based on these news search results, create a comprehensive visual summary:
|
| 648 |
|
| 649 |
+
Original Query: {query}
|
| 650 |
|
|
|
|
| 651 |
{search_summary}
|
| 652 |
|
| 653 |
+
Please create a visually appealing HTML response that:
|
| 654 |
+
1. Summarizes the key points from the news results
|
| 655 |
+
2. Organizes information in a clear and structured way
|
| 656 |
+
3. Uses appropriate styling for better readability
|
| 657 |
+
4. Includes important quotes and facts
|
| 658 |
+
5. Cites sources properly
|
| 659 |
|
| 660 |
+
Make sure to maintain proper HTML formatting in the response.
|
| 661 |
"""
|
| 662 |
|
| 663 |
+
print("Generating response with enhanced prompt...") # ๋๋ฒ๊น
์ฉ
|
| 664 |
+
|
| 665 |
# async generator๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์์
|
| 666 |
async for result in demo_instance.generation_code(enhanced_prompt, setting):
|
| 667 |
final_result = result
|
| 668 |
+
|
| 669 |
+
print("Response generation completed") # ๋๋ฒ๊น
์ฉ
|
| 670 |
return final_result
|
| 671 |
|
| 672 |
except Exception as e:
|
| 673 |
+
print(f"Search error: {str(e)}") # ๋๋ฒ๊น
์ฉ
|
| 674 |
+
print(f"Full error details: {str(e.__class__.__name__)}: {str(e)}") # ๋๋ฒ๊น
์ฉ
|
| 675 |
return [
|
| 676 |
"",
|
| 677 |
None,
|
| 678 |
gr.update(active_key="error"),
|
| 679 |
gr.update(open=False)
|
| 680 |
]
|
| 681 |
+
|
| 682 |
def execute_code(query: str):
|
| 683 |
if not query or query.strip() == '':
|
| 684 |
return None, gr.update(active_key="empty")
|