Spaces:
Sleeping
Sleeping
File size: 2,461 Bytes
6cf49f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | # =========================================================
# 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()
|