Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,80 +5,56 @@ import warnings
|
|
5 |
|
6 |
warnings.filterwarnings("ignore")
|
7 |
|
8 |
-
#
|
9 |
-
st.set_page_config(
|
10 |
-
page_title="DuckDuckGo Search Tool",
|
11 |
-
page_icon="π",
|
12 |
-
layout="wide",
|
13 |
-
)
|
14 |
-
|
15 |
-
# Function to initialize the API with user-provided token
|
16 |
-
def initialize_api(token=None):
|
17 |
-
if token:
|
18 |
-
login(token)
|
19 |
-
try:
|
20 |
-
return HfApiModel(model_id="Qwen/Qwen2.5-32B-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 |
-
|
31 |
-
st.
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
with st.sidebar:
|
42 |
st.title("Configuration")
|
43 |
-
|
44 |
-
"Hugging Face
|
45 |
-
value=st.session_state.hf_token,
|
46 |
type="password",
|
47 |
-
|
48 |
)
|
49 |
|
50 |
-
if
|
51 |
-
st.session_state.hf_token =
|
52 |
-
st.session_state.model = None
|
53 |
-
|
54 |
if st.button("Initialize API"):
|
55 |
-
with st.spinner("
|
56 |
-
|
57 |
-
|
58 |
-
st.session_state.model =
|
59 |
-
st.success("
|
60 |
-
|
61 |
-
st.error("
|
62 |
|
63 |
# Main app logic
|
64 |
if st.session_state.model:
|
65 |
-
st.
|
66 |
-
query = st.text_input("
|
67 |
|
68 |
-
if st.button("Search")
|
69 |
with st.spinner("Searching..."):
|
70 |
agent = CodeAgent(tools=[search_tool], model=st.session_state.model)
|
71 |
response = agent.run(query)
|
72 |
-
st.
|
73 |
-
st.markdown("### Results")
|
74 |
-
st.markdown(response)
|
75 |
else:
|
76 |
-
st.info("Please enter your Hugging Face
|
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
|
|
|
5 |
|
6 |
warnings.filterwarnings("ignore")
|
7 |
|
8 |
+
# Initialize tools first (outside session state)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
search_tool = DuckDuckGoSearchTool()
|
10 |
|
11 |
def main():
|
12 |
+
# Streamlit config
|
13 |
+
st.set_page_config(
|
14 |
+
page_title="DuckDuckGo Search Tool",
|
15 |
+
page_icon="π",
|
16 |
+
layout="wide",
|
17 |
+
)
|
18 |
+
|
19 |
+
# Session state initialization
|
20 |
+
if "model" not in st.session_state:
|
21 |
+
st.session_state.model = None
|
22 |
+
st.session_state.hf_token = ""
|
23 |
+
|
24 |
+
# Sidebar logic
|
25 |
with st.sidebar:
|
26 |
st.title("Configuration")
|
27 |
+
new_token = st.text_input(
|
28 |
+
"Hugging Face Token",
|
|
|
29 |
type="password",
|
30 |
+
value=st.session_state.hf_token
|
31 |
)
|
32 |
|
33 |
+
if new_token != st.session_state.hf_token:
|
34 |
+
st.session_state.hf_token = new_token
|
35 |
+
st.session_state.model = None # Reset model on token change
|
36 |
+
|
37 |
if st.button("Initialize API"):
|
38 |
+
with st.spinner("Initializing..."):
|
39 |
+
try:
|
40 |
+
login(st.session_state.hf_token)
|
41 |
+
st.session_state.model = HfApiModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
|
42 |
+
st.success("API Initialized!")
|
43 |
+
except Exception as e:
|
44 |
+
st.error(f"Error: {str(e)}")
|
45 |
|
46 |
# Main app logic
|
47 |
if st.session_state.model:
|
48 |
+
st.title("AI Web Search")
|
49 |
+
query = st.text_input("Enter your search query")
|
50 |
|
51 |
+
if query and st.button("Search"):
|
52 |
with st.spinner("Searching..."):
|
53 |
agent = CodeAgent(tools=[search_tool], model=st.session_state.model)
|
54 |
response = agent.run(query)
|
55 |
+
st.markdown(f"## Results\n{response}")
|
|
|
|
|
56 |
else:
|
57 |
+
st.info("Please enter your Hugging Face token in the sidebar")
|
|
|
58 |
|
|
|
59 |
if __name__ == "__main__":
|
60 |
+
main() # No infinite loop needed when using proper streamlit run
|
|
|
|
|
|