taha-the-data-scientist commited on
Commit
30a05e4
Β·
verified Β·
1 Parent(s): a103876

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import streamlit as st
3
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
4
+ from huggingface_hub import login
5
+ import warnings
6
+
7
+ warnings.filterwarnings("ignore")
8
+
9
+ # Set page configuration
10
+ st.set_page_config(
11
+ page_title="DuckDuckGo Search Tool",
12
+ page_icon="πŸ”",
13
+ layout="wide",
14
+ )
15
+
16
+ # Function to initialize the API with user-provided token
17
+ 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
+ """)