Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# app.py
|
2 |
import streamlit as st
|
3 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
4 |
from huggingface_hub import login
|
@@ -18,88 +17,68 @@ def initialize_api(token=None):
|
|
18 |
if token:
|
19 |
login(token)
|
20 |
try:
|
21 |
-
# Initialize the model
|
22 |
return HfApiModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
|
23 |
except Exception as e:
|
24 |
st.error(f"Error initializing model: {str(e)}")
|
25 |
return None
|
26 |
-
|
27 |
-
st.warning("Please enter your Hugging Face API token to use this app.")
|
28 |
-
return None
|
29 |
|
30 |
# Initialize tools
|
31 |
search_tool = DuckDuckGoSearchTool()
|
32 |
|
33 |
-
|
34 |
-
st.title("DuckDuckGo Search Tool")
|
35 |
-
st.markdown("Search the web using DuckDuckGo and get AI-powered responses")
|
36 |
-
|
37 |
-
# Initialize session state for token
|
38 |
-
if "hf_token" not in st.session_state:
|
39 |
-
st.session_state.hf_token = ""
|
40 |
-
st.session_state.model = None
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
"Enter your Hugging Face API Token:",
|
49 |
-
value=st.session_state.hf_token,
|
50 |
-
type="password",
|
51 |
-
help="Get your token from huggingface.co/settings/tokens"
|
52 |
-
)
|
53 |
-
|
54 |
-
if token_input != st.session_state.hf_token:
|
55 |
-
st.session_state.hf_token = token_input
|
56 |
-
# Reset model when token changes
|
57 |
-
st.session_state.model = None
|
58 |
-
|
59 |
-
# Button to initialize/test the token
|
60 |
-
if st.button("Initialize API"):
|
61 |
-
with st.spinner("Testing your token..."):
|
62 |
-
model = initialize_api(st.session_state.hf_token)
|
63 |
-
if model:
|
64 |
-
st.session_state.model = model
|
65 |
-
st.success("β
API initialized successfully!")
|
66 |
-
else:
|
67 |
-
st.error("β Failed to initialize the API with the provided token.")
|
68 |
|
69 |
-
#
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
# Main
|
77 |
-
if st.session_state.
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
if st.button("Search"):
|
84 |
-
if query:
|
85 |
-
with st.spinner("Searching and processing..."):
|
86 |
agent = CodeAgent(tools=[search_tool], model=st.session_state.model)
|
87 |
response = agent.run(query)
|
88 |
st.success("Search complete!")
|
89 |
st.markdown("### Results")
|
90 |
st.markdown(response)
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
1. Go to [huggingface.co](https://huggingface.co/) and create an account if you don't have one
|
102 |
-
2. Visit your [settings page](https://huggingface.co/settings/tokens)
|
103 |
-
3. Create a new token with read access
|
104 |
-
4. Copy the token and paste it in the sidebar
|
105 |
-
""")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
3 |
from huggingface_hub import login
|
|
|
17 |
if token:
|
18 |
login(token)
|
19 |
try:
|
|
|
20 |
return HfApiModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
|
21 |
except Exception as e:
|
22 |
st.error(f"Error initializing model: {str(e)}")
|
23 |
return None
|
24 |
+
return None
|
|
|
|
|
25 |
|
26 |
# Initialize tools
|
27 |
search_tool = DuckDuckGoSearchTool()
|
28 |
|
29 |
+
def main():
|
30 |
+
st.title("DuckDuckGo Search Tool")
|
31 |
+
st.markdown("Search the web using DuckDuckGo and get AI-powered responses")
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
# Initialize session state
|
34 |
+
if "hf_token" not in st.session_state:
|
35 |
+
st.session_state.update({
|
36 |
+
"hf_token": "",
|
37 |
+
"model": None
|
38 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
# Sidebar configuration
|
41 |
+
with st.sidebar:
|
42 |
+
st.title("Configuration")
|
43 |
+
token_input = st.text_input(
|
44 |
+
"Hugging Face API Token:",
|
45 |
+
value=st.session_state.hf_token,
|
46 |
+
type="password",
|
47 |
+
help="Get token from huggingface.co/settings/tokens"
|
48 |
+
)
|
49 |
+
|
50 |
+
if token_input != st.session_state.hf_token:
|
51 |
+
st.session_state.hf_token = token_input
|
52 |
+
st.session_state.model = None
|
53 |
+
|
54 |
+
if st.button("Initialize API"):
|
55 |
+
with st.spinner("Testing token..."):
|
56 |
+
model = initialize_api(st.session_state.hf_token)
|
57 |
+
if model:
|
58 |
+
st.session_state.model = model
|
59 |
+
st.success("β
API initialized!")
|
60 |
+
else:
|
61 |
+
st.error("β Initialization failed")
|
62 |
|
63 |
+
# Main app logic
|
64 |
+
if st.session_state.model:
|
65 |
+
st.header("AI Web Search")
|
66 |
+
query = st.text_input("Search query:", placeholder="Latest renewable energy advancements?")
|
67 |
+
|
68 |
+
if st.button("Search") and query:
|
69 |
+
with st.spinner("Searching..."):
|
|
|
|
|
|
|
70 |
agent = CodeAgent(tools=[search_tool], model=st.session_state.model)
|
71 |
response = agent.run(query)
|
72 |
st.success("Search complete!")
|
73 |
st.markdown("### Results")
|
74 |
st.markdown(response)
|
75 |
+
else:
|
76 |
+
st.info("Please enter your Hugging Face API token in the sidebar")
|
77 |
+
# ... rest of your info message ...
|
78 |
+
|
79 |
+
# Hugging Face Spaces requires this specific structure
|
80 |
+
if __name__ == "__main__":
|
81 |
+
main()
|
82 |
+
# Keep the server running
|
83 |
+
while True:
|
84 |
+
pass
|
|
|
|
|
|
|
|
|
|