Spaces:
Sleeping
Sleeping
| # ========================================================= | |
| # Hariharan Subramanyan's Search Bot | |
| # Hugging Face Spaces - app.py | |
| # ========================================================= | |
| import os | |
| import gradio as gr | |
| from langchain_groq import ChatGroq | |
| from langchain_tavily import TavilySearch | |
| # ========================================================= | |
| # Step 1: Load API Keys from Hugging Face Secrets | |
| # ========================================================= | |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY") | |
| TAVILY_API_KEY = os.environ.get("TAVILY_API_KEY") | |
| if not GROQ_API_KEY: | |
| raise ValueError("GROQ_API_KEY is missing. Add it in Hugging Face Space Secrets.") | |
| if not TAVILY_API_KEY: | |
| raise ValueError("TAVILY_API_KEY is missing. Add it in Hugging Face Space Secrets.") | |
| # ========================================================= | |
| # Step 2: Load LLM and Search Tool | |
| # ========================================================= | |
| llm = ChatGroq( | |
| model_name="openai/gpt-oss-120b", | |
| temperature=0, | |
| groq_api_key=GROQ_API_KEY | |
| ) | |
| search = TavilySearch( | |
| max_results=5, | |
| tavily_api_key=TAVILY_API_KEY | |
| ) | |
| # ========================================================= | |
| # Step 3: Search + Answer Function | |
| # ========================================================= | |
| def search_bot(query): | |
| if not query.strip(): | |
| return "Please enter a search query." | |
| # Perform web search | |
| results = search.invoke(query) | |
| # Extract content from search results | |
| context = "\n".join( | |
| [r["content"] for r in results.get("results", [])] | |
| ) | |
| if not context: | |
| return "I couldn't find relevant information. Try another query." | |
| # Build final prompt | |
| final_prompt = f""" | |
| Use the following information to answer clearly and accurately. | |
| Information: | |
| {context} | |
| Question: | |
| {query} | |
| Answer: | |
| """ | |
| response = llm.invoke(final_prompt) | |
| return response.content.strip() | |
| # ========================================================= | |
| # Step 4: Gradio UI | |
| # ========================================================= | |
| demo = gr.Interface( | |
| fn=search_bot, | |
| inputs=gr.Textbox( | |
| label="Search Query", | |
| placeholder="Example: Scorecard of IPL 2025 Final" | |
| ), | |
| outputs=gr.Textbox(label="Answer"), | |
| title="🔎 AI Search Bot", | |
| description="Powered by Tavily Search + Groq LLM\nBuilt at Hariharan Subramanyan AI – Artificial Intelligence Research Institute", | |
| theme="soft" | |
| ) | |
| demo.launch() | |