taha-the-data-scientist commited on
Commit
9ab2545
Β·
verified Β·
1 Parent(s): 79cf6bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -70
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
- else:
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
- # Streamlit App
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
- # Sidebar for token input
43
- with st.sidebar:
44
- st.title("Configuration")
45
-
46
- # Token input
47
- token_input = st.text_input(
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
- # Check if the model is initialized
70
- if not st.session_state.model and st.session_state.hf_token:
71
- with st.spinner("Initializing model..."):
72
- st.session_state.model = initialize_api(st.session_state.hf_token)
73
- if st.session_state.model:
74
- st.success("Model initialized successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- # Main content area - only show if token is provided
77
- if st.session_state.hf_token and st.session_state.model:
78
- st.header("AI Web Search")
79
- st.markdown("Ask any question and the AI will search the web for answers")
80
-
81
- query = st.text_input("Enter your search query:", placeholder="What are the latest advancements in renewable energy?")
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
- else:
92
- st.warning("Please enter a search query")
93
- else:
94
- # Token not provided
95
- st.info("Please enter your Hugging Face API token in the sidebar to get started.")
96
-
97
- # Show more information to help users understand what they need
98
- st.markdown("""
99
- ### How to Get a Hugging Face Token
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