openfree commited on
Commit
f101e49
ยท
verified ยท
1 Parent(s): 0bd1248

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -58
app.py CHANGED
@@ -567,28 +567,15 @@ def update_placeholder():
567
  return gr.update(placeholder=get_random_placeholder())
568
 
569
 
570
-
571
- def create_main_interface():
572
- """๋ฉ”์ธ ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ ํ•จ์ˆ˜"""
573
-
574
- async def handle_generation(query, setting, is_search):
575
- try:
576
- print(f"Mode: {'Web Search' if is_search else 'Generate'}") # ๋””๋ฒ„๊น…์šฉ
577
- if is_search:
578
- print("Executing search and generate...") # ๋””๋ฒ„๊น…์šฉ
579
- return await execute_search_and_generate(query, setting)
580
- else:
581
- print("Executing normal generation...") # ๋””๋ฒ„๊น…์šฉ
582
- async for result in demo_instance.generation_code(query, setting):
583
- final_result = result
584
- return final_result
585
- except Exception as e:
586
- print(f"Generation error: {str(e)}")
587
- return ["", None, gr.update(active_key="error"), gr.update(open=False)]
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"
@@ -601,9 +588,7 @@ def create_main_interface():
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 = {
@@ -611,67 +596,57 @@ def create_main_interface():
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,
@@ -694,7 +669,23 @@ Make sure to maintain proper HTML formatting in the response.
694
  print(f"Error executing code: {str(e)}")
695
  return None, gr.update(active_key="empty")
696
 
 
697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
698
 
699
  # CSS ํŒŒ์ผ ๋‚ด์šฉ์„ ์ง์ ‘ ์ ์šฉ
700
  with open('app.css', 'r', encoding='utf-8') as f:
 
567
  return gr.update(placeholder=get_random_placeholder())
568
 
569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
 
571
+
572
+ def create_main_interface():
573
+ """๋ฉ”์ธ ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ ํ•จ์ˆ˜"""
574
+
575
+ #NEW - ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ํ†ตํ•ฉํ•œ ์‘๋‹ต ์ƒ์„ฑ ํ•จ์ˆ˜
576
+ async def execute_search_and_generate(query, setting):
577
  try:
578
+ print(f"Executing search for query: {query}")
579
 
580
  # ๊ฒ€์ƒ‰ ์‹คํ–‰
581
  url = "https://api.serphouse.com/serp/live"
 
588
  "serp_type": "news",
589
  "loc": "United States",
590
  "page": "1",
591
+ "num": "10"
 
 
592
  }
593
  }
594
  headers = {
 
596
  "Content-Type": "application/json"
597
  }
598
 
 
599
  response = requests.post(url, headers=headers, json=payload)
 
 
 
 
 
 
600
  results = response.json()
601
+ print(f"Search results: {results}") # ๋””๋ฒ„๊น…์šฉ
602
 
603
+ # ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ HTML๋กœ ๋ณ€ํ™˜
604
+ search_content = "```html\n<div class='search-results'>\n"
605
+ search_content += "<h2>์ตœ์‹  ๋‰ด์Šค ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ</h2>\n"
606
 
607
+ # API ์‘๋‹ต ๊ตฌ์กฐ์— ๋งž๊ฒŒ ์ˆ˜์ •
608
  if 'results' in results:
609
+ news_items = results['results'].get('news', [])
610
+ for item in news_items[:5]:
611
+ search_content += f"""
 
 
612
  <div class="search-item">
613
+ <h3><a href="{item['url']}" target="_blank">{item['title']}</a></h3>
614
+ <p>{item['snippet']}</p>
615
  <div class="search-meta">
616
+ <span class="source">{item['channel']}</span>
617
+ <span class="time">{item['time']}</span>
618
  </div>
619
  </div>
620
  """
621
+ search_content += "</div>\n```"
622
 
623
+ # ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ํฌํ•จํ•œ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ
624
  enhanced_prompt = f"""Based on these news search results, create a comprehensive visual summary:
625
 
626
+ {search_content}
 
 
627
 
628
  Please create a visually appealing HTML response that:
629
+ 1. Summarizes the key points from the news
630
+ 2. Organizes information in a clear structure
631
+ 3. Uses appropriate HTML formatting and styling
632
+ 4. Includes relevant quotes and statistics
633
+ 5. Provides proper source attribution
634
 
635
+ The response should be in HTML format with appropriate styling."""
 
636
 
637
+ print("Generating response with search results...") # ๋””๋ฒ„๊น…์šฉ
638
 
639
  # async generator๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜์ •
640
  async for result in demo_instance.generation_code(enhanced_prompt, setting):
641
  final_result = result
642
+ print(f"Generated result: {final_result}") # ๋””๋ฒ„๊น…์šฉ
643
 
644
  print("Response generation completed") # ๋””๋ฒ„๊น…์šฉ
645
  return final_result
646
 
647
  except Exception as e:
648
+ print(f"Search error: {str(e)}")
649
+ print(f"Full error details: {str(e.__class__.__name__)}: {str(e)}")
650
  return [
651
  "",
652
  None,
 
669
  print(f"Error executing code: {str(e)}")
670
  return None, gr.update(active_key="empty")
671
 
672
+
673
 
674
+
675
+ async def handle_generation(query, setting, is_search):
676
+ try:
677
+ print(f"Mode: {'Web Search' if is_search else 'Generate'}") # ๋””๋ฒ„๊น…์šฉ
678
+ if is_search:
679
+ print("Executing search and generate...") # ๋””๋ฒ„๊น…์šฉ
680
+ return await execute_search_and_generate(query, setting)
681
+ else:
682
+ print("Executing normal generation...") # ๋””๋ฒ„๊น…์šฉ
683
+ async for result in demo_instance.generation_code(query, setting):
684
+ final_result = result
685
+ return final_result
686
+ except Exception as e:
687
+ print(f"Generation error: {str(e)}")
688
+ return ["", None, gr.update(active_key="error"), gr.update(open=False)]
689
 
690
  # CSS ํŒŒ์ผ ๋‚ด์šฉ์„ ์ง์ ‘ ์ ์šฉ
691
  with open('app.css', 'r', encoding='utf-8') as f: