taha-the-data-scientist commited on
Commit
c2e2156
Β·
verified Β·
1 Parent(s): c6fea78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -58
app.py CHANGED
@@ -5,80 +5,56 @@ import warnings
5
 
6
  warnings.filterwarnings("ignore")
7
 
8
- # Set page configuration
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
- 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
 
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