Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import os
|
2 |
import time
|
3 |
import streamlit as st
|
4 |
-
from smolagents import
|
5 |
from huggingface_hub import InferenceClient
|
6 |
-
|
7 |
-
|
8 |
-
search_tool = DuckDuckGoSearchTool()
|
9 |
|
10 |
# Retrieve Hugging Face token
|
11 |
hf_token = os.getenv("HF_TOKEN")
|
@@ -15,11 +14,65 @@ if not hf_token:
|
|
15 |
# Initialize the Hugging Face Inference client
|
16 |
client = InferenceClient(token=hf_token)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Initialize the SmolAgent model
|
19 |
model = HfApiModel(model_id="meta-llama/Llama-3.2-3B-Instruct", token=hf_token)
|
20 |
|
21 |
-
# Create the agent
|
22 |
-
agent = CodeAgent(tools=[
|
23 |
|
24 |
def apply_custom_styles():
|
25 |
st.markdown(
|
@@ -86,17 +139,12 @@ def main():
|
|
86 |
st.session_state["state_reset"] = False
|
87 |
|
88 |
def process_input():
|
89 |
-
"""
|
90 |
user_input = st.session_state["user_input"]
|
91 |
if user_input.strip():
|
92 |
try:
|
93 |
-
# Instruct Tom Riddle to use the search tool only when uncertain, efficiently.
|
94 |
response = agent.run(
|
95 |
-
f"You are Tom Riddle, a cunning and enigmatic character from Harry Potter. "
|
96 |
-
f"Answer the user's query clearly and concisely in your distinct persona. "
|
97 |
-
f"If you are not certain of your answer, only then use your available DuckDuckGo search tool to quickly gather the necessary insights—doing so efficiently without causing delays. "
|
98 |
-
f"Finally, produce your answer in your characteristic voice without revealing your internal process. "
|
99 |
-
f"User Query: {user_input}"
|
100 |
)
|
101 |
st.session_state["response"] = response
|
102 |
st.session_state["waiting_for_input"] = False
|
@@ -124,4 +172,4 @@ def main():
|
|
124 |
st.rerun()
|
125 |
|
126 |
if __name__ == "__main__":
|
127 |
-
main()
|
|
|
1 |
import os
|
2 |
import time
|
3 |
import streamlit as st
|
4 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
5 |
from huggingface_hub import InferenceClient
|
6 |
+
import requests
|
7 |
+
from bs4 import BeautifulSoup
|
|
|
8 |
|
9 |
# Retrieve Hugging Face token
|
10 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
14 |
# Initialize the Hugging Face Inference client
|
15 |
client = InferenceClient(token=hf_token)
|
16 |
|
17 |
+
# Custom tools for SmolAgents
|
18 |
+
@tool
|
19 |
+
def search_harry_potter_lore(query: str) -> str:
|
20 |
+
"""Search for Harry Potter-related lore or facts across the entire Harry Potter Fandom site.
|
21 |
+
|
22 |
+
Args:
|
23 |
+
query: A specific question or topic about Harry Potter lore.
|
24 |
+
|
25 |
+
Returns:
|
26 |
+
A concise and informative response based on the query.
|
27 |
+
"""
|
28 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
29 |
+
# Construct the search URL for the Harry Potter Fandom site.
|
30 |
+
search_url = f"https://harrypotter.fandom.com/wiki/Special:Search?query={query}"
|
31 |
+
|
32 |
+
try:
|
33 |
+
# Fetch the search results page.
|
34 |
+
search_response = requests.get(search_url, headers=headers)
|
35 |
+
if search_response.status_code != 200:
|
36 |
+
return f"Error: Received status code {search_response.status_code} from search."
|
37 |
+
|
38 |
+
search_soup = BeautifulSoup(search_response.text, 'html.parser')
|
39 |
+
|
40 |
+
# Look for the first link that appears to be an article.
|
41 |
+
article_url = None
|
42 |
+
for a in search_soup.find_all("a", href=True):
|
43 |
+
href = a["href"]
|
44 |
+
# We want links that start with /wiki/ but skip those that contain "Special:"
|
45 |
+
if href.startswith("/wiki/") and "Special:" not in href:
|
46 |
+
article_url = "https://harrypotter.fandom.com" + href
|
47 |
+
break
|
48 |
+
|
49 |
+
if not article_url:
|
50 |
+
return "No results found for your query."
|
51 |
+
|
52 |
+
# Fetch the article page.
|
53 |
+
article_response = requests.get(article_url, headers=headers)
|
54 |
+
if article_response.status_code != 200:
|
55 |
+
return f"Error: Received status code {article_response.status_code} from the article page."
|
56 |
+
|
57 |
+
article_soup = BeautifulSoup(article_response.text, 'html.parser')
|
58 |
+
|
59 |
+
# Extract the first meaningful paragraph.
|
60 |
+
paragraphs = article_soup.find_all("p")
|
61 |
+
for p in paragraphs:
|
62 |
+
text = p.get_text().strip()
|
63 |
+
if len(text) > 50: # A simple threshold to ensure the paragraph is informative.
|
64 |
+
return text
|
65 |
+
|
66 |
+
return "Couldn't extract detailed lore from the article."
|
67 |
+
|
68 |
+
except Exception as e:
|
69 |
+
return f"An error occurred: {str(e)}"
|
70 |
+
|
71 |
# Initialize the SmolAgent model
|
72 |
model = HfApiModel(model_id="meta-llama/Llama-3.2-3B-Instruct", token=hf_token)
|
73 |
|
74 |
+
# Create the agent
|
75 |
+
agent = CodeAgent(tools=[search_harry_potter_lore], model=model)
|
76 |
|
77 |
def apply_custom_styles():
|
78 |
st.markdown(
|
|
|
139 |
st.session_state["state_reset"] = False
|
140 |
|
141 |
def process_input():
|
142 |
+
"""Fetch Tom Riddle's response based on user input."""
|
143 |
user_input = st.session_state["user_input"]
|
144 |
if user_input.strip():
|
145 |
try:
|
|
|
146 |
response = agent.run(
|
147 |
+
f"You are Tom Riddle, a cunning and enigmatic character from Harry Potter. Respond concisely and pragmatically and remain true to your role in the series: {user_input}"
|
|
|
|
|
|
|
|
|
148 |
)
|
149 |
st.session_state["response"] = response
|
150 |
st.session_state["waiting_for_input"] = False
|
|
|
172 |
st.rerun()
|
173 |
|
174 |
if __name__ == "__main__":
|
175 |
+
main()
|