Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
from bs4 import BeautifulSoup
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
-
# Initialize Hugging Face client
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
headers = {"User-Agent": "Mozilla/5.0"}
|
| 14 |
-
search_url = f"https://www.google.com/search?q={search_query}"
|
| 15 |
-
response = requests.get(search_url, headers=headers)
|
| 16 |
-
soup = BeautifulSoup(response.text, "html.parser")
|
| 17 |
-
# Extract the first few search results (as plain text)
|
| 18 |
-
results = [a.text for a in soup.find_all('h3')[:3]]
|
| 19 |
-
return results if results else ["No results found."]
|
| 20 |
-
except Exception as e:
|
| 21 |
-
return [f"Error in Web Search: {str(e)}"]
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
for content in content_list:
|
| 27 |
-
try:
|
| 28 |
-
response = ""
|
| 29 |
-
for message in client.chat_completion(
|
| 30 |
-
[{"role": "user", "content": f"Summarize this: {content}"}],
|
| 31 |
-
max_tokens=100,
|
| 32 |
-
stream=True,
|
| 33 |
-
temperature=0.7,
|
| 34 |
-
top_p=0.95,
|
| 35 |
-
):
|
| 36 |
-
response += message.choices[0].delta.content
|
| 37 |
-
summaries.append(response)
|
| 38 |
-
except Exception as e:
|
| 39 |
-
summaries.append(f"Error summarizing content: {str(e)}")
|
| 40 |
-
return summaries
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
final_analysis = " ".join(summaries)
|
| 46 |
-
response = ""
|
| 47 |
-
for message in client.chat_completion(
|
| 48 |
-
[{"role": "user", "content": f"Provide a detailed overview: {final_analysis}"}],
|
| 49 |
-
max_tokens=200,
|
| 50 |
-
stream=True,
|
| 51 |
-
temperature=0.7,
|
| 52 |
-
top_p=0.95,
|
| 53 |
-
):
|
| 54 |
-
response += message.choices[0].delta.content
|
| 55 |
-
return response
|
| 56 |
-
except Exception as e:
|
| 57 |
-
return f"Error in final analysis: {str(e)}"
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
def
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
|
|
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
with gr.Row():
|
| 84 |
-
gr.Markdown("### Web Search Results")
|
| 85 |
-
search_results_output = gr.Textbox(label="Bot 1: Search Results", lines=6)
|
| 86 |
-
|
| 87 |
-
with gr.Row():
|
| 88 |
-
gr.Markdown("### Summarized Content")
|
| 89 |
-
summaries_output = gr.Textbox(label="Bot 2: Summaries", lines=6)
|
| 90 |
-
|
| 91 |
-
with gr.Row():
|
| 92 |
-
gr.Markdown("### Final Analysis")
|
| 93 |
-
final_output = gr.Textbox(label="Bot 3: Final Overview", lines=6)
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
process_button.click(
|
| 97 |
-
process_workflow,
|
| 98 |
-
inputs=[search_query],
|
| 99 |
-
outputs={
|
| 100 |
-
"search_results": search_results_output,
|
| 101 |
-
"summaries": summaries_output,
|
| 102 |
-
"final_analysis": final_output,
|
| 103 |
-
},
|
| 104 |
-
)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Initialize Hugging Face client with your model
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# Define the task for each bot
|
| 8 |
+
def search_web(query):
|
| 9 |
+
# Simulating web search (replace with actual search logic)
|
| 10 |
+
return f"Searching for: {query}..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def summarize_content(content):
|
| 13 |
+
# Simulating summarization (replace with actual summarization logic)
|
| 14 |
+
return f"Summary: {content[:50]}..." # Simple truncation for demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def final_review(summary):
|
| 17 |
+
# Simulating final review of the summary (replace with actual review logic)
|
| 18 |
+
return f"Final Overview: {summary}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Define the process button callback function
|
| 21 |
+
def process_task(query, history):
|
| 22 |
+
# 1. First bot searches the web
|
| 23 |
+
search_result = search_web(query)
|
| 24 |
+
|
| 25 |
+
# 2. Second bot summarizes the content
|
| 26 |
+
summary = summarize_content(search_result)
|
| 27 |
+
|
| 28 |
+
# 3. Third bot gives the final review
|
| 29 |
+
final_overview = final_review(summary)
|
| 30 |
+
|
| 31 |
+
# Return results for each step (this will be reflected in the outputs)
|
| 32 |
+
return search_result, summary, final_overview
|
| 33 |
|
| 34 |
+
# Set up the Gradio interface
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
+
# Create input component for user to type in a query
|
| 37 |
+
query_input = gr.Textbox(label="Enter your search query")
|
| 38 |
|
| 39 |
+
# Create output components for search result, summary, and final review
|
| 40 |
+
search_output = gr.Textbox(label="Search Result")
|
| 41 |
+
summary_output = gr.Textbox(label="Summary")
|
| 42 |
+
final_output = gr.Textbox(label="Final Review")
|
| 43 |
|
| 44 |
+
# Create process button to start the task
|
| 45 |
+
process_button = gr.Button("Process Task")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Define the button click action
|
| 48 |
+
process_button.click(process_task, inputs=[query_input, None], outputs=[search_output, summary_output, final_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Launch the Gradio interface
|
| 51 |
+
demo.launch()
|