Spaces:
Sleeping
Sleeping
Update 1 app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -14,39 +39,55 @@ def respond(
|
|
| 14 |
max_tokens,
|
| 15 |
temperature,
|
| 16 |
top_p,
|
|
|
|
| 17 |
):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
gr.Slider(
|
|
@@ -56,9 +97,15 @@ demo = gr.ChatInterface(
|
|
| 56 |
step=0.05,
|
| 57 |
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, VisitWebpageTool, HfApiModel
|
| 4 |
|
| 5 |
+
# Initialize HuggingFace client
|
|
|
|
|
|
|
| 6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 7 |
|
| 8 |
+
# Create the smolagents agent (without UserInputTool since we'll get input from Gradio)
|
| 9 |
+
agent = CodeAgent(
|
| 10 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), FinalAnswerTool()],
|
| 11 |
+
model=HfApiModel(),
|
| 12 |
+
max_steps=8,
|
| 13 |
+
verbosity_level=1
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Function to perform web research with a provided query
|
| 17 |
+
def research_with_query(query):
|
| 18 |
+
result = agent.run(f"""
|
| 19 |
+
Think step by step:
|
| 20 |
+
|
| 21 |
+
1. The user has asked about: "{query}"
|
| 22 |
+
2. Use the DuckDuckGoSearchTool to search the web for information about this query.
|
| 23 |
+
3. From the search results, identify 1-2 relevant webpage URLs that might contain detailed information.
|
| 24 |
+
4. Use the VisitWebpageTool to visit each identified webpage and extract its content.
|
| 25 |
+
5. Combine the information from the search results and webpage visits.
|
| 26 |
+
6. Create a comprehensive bullet point summary of all collected information.
|
| 27 |
+
7. Each bullet point should start with "• " and be on a new line.
|
| 28 |
+
8. Use the FinalAnswerTool to present your bullet-point summary as the final answer.
|
| 29 |
+
|
| 30 |
+
Make sure your bullet points are clear, well-organized, and directly relevant to the user's query.
|
| 31 |
+
Include the most important and factual information from your research.
|
| 32 |
+
""")
|
| 33 |
+
return result
|
| 34 |
|
| 35 |
def respond(
|
| 36 |
message,
|
|
|
|
| 39 |
max_tokens,
|
| 40 |
temperature,
|
| 41 |
top_p,
|
| 42 |
+
use_web_search,
|
| 43 |
):
|
| 44 |
+
# Check if web search is enabled and message starts with a research request
|
| 45 |
+
if use_web_search and message.strip().lower().startswith(("search:", "research:", "find info:")):
|
| 46 |
+
query = message.split(":", 1)[1].strip()
|
| 47 |
+
|
| 48 |
+
yield "Searching the web for information about your query. This may take a moment..."
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
# Perform the web search and get bullet point summary
|
| 52 |
+
research_results = research_with_query(query)
|
| 53 |
+
|
| 54 |
+
# Return the research results
|
| 55 |
+
yield f"Here's what I found about '{query}':\n\n{research_results}"
|
| 56 |
+
except Exception as e:
|
| 57 |
+
yield f"Sorry, I encountered an error while searching the web: {str(e)}"
|
| 58 |
+
else:
|
| 59 |
+
# Regular chat completion for normal messages
|
| 60 |
+
messages = [{"role": "system", "content": system_message}]
|
| 61 |
|
| 62 |
+
for val in history:
|
| 63 |
+
if val[0]:
|
| 64 |
+
messages.append({"role": "user", "content": val[0]})
|
| 65 |
+
if val[1]:
|
| 66 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 67 |
|
| 68 |
+
messages.append({"role": "user", "content": message})
|
| 69 |
|
| 70 |
+
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
for message in client.chat_completion(
|
| 73 |
+
messages,
|
| 74 |
+
max_tokens=max_tokens,
|
| 75 |
+
stream=True,
|
| 76 |
+
temperature=temperature,
|
| 77 |
+
top_p=top_p,
|
| 78 |
+
):
|
| 79 |
+
token = message.choices[0].delta.content
|
| 80 |
+
response += token
|
| 81 |
+
yield response
|
| 82 |
|
| 83 |
+
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
| 84 |
demo = gr.ChatInterface(
|
| 85 |
respond,
|
| 86 |
additional_inputs=[
|
| 87 |
+
gr.Textbox(
|
| 88 |
+
value="You are a helpful assistant. When users ask you to search for information with 'search:', 'research:', or 'find info:', you will search the web for them.",
|
| 89 |
+
label="System message"
|
| 90 |
+
),
|
| 91 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 92 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 93 |
gr.Slider(
|
|
|
|
| 97 |
step=0.05,
|
| 98 |
label="Top-p (nucleus sampling)",
|
| 99 |
),
|
| 100 |
+
gr.Checkbox(value=True, label="Enable web search (use 'search:', 'research:', or 'find info:' to search)")
|
| 101 |
+
],
|
| 102 |
+
examples=[
|
| 103 |
+
["search: latest developments in quantum computing"],
|
| 104 |
+
["research: climate change impacts in 2023"],
|
| 105 |
+
["find info: benefits of meditation"],
|
| 106 |
+
["Hello! How are you today?"]
|
| 107 |
],
|
| 108 |
)
|
| 109 |
|
|
|
|
| 110 |
if __name__ == "__main__":
|
| 111 |
+
demo.launch()
|