Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,18 @@ import gradio as gr
|
|
| 3 |
from langchain_groq import ChatGroq
|
| 4 |
from langchain_tavily import TavilySearch
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
groq_api_key = os.environ.get("GROQ_API_KEY")
|
| 7 |
tavily_api_key = os.environ.get("TAVILY_API_KEY")
|
| 8 |
|
| 9 |
-
if not groq_api_key:
|
| 10 |
-
raise
|
| 11 |
-
if not tavily_api_key:
|
| 12 |
-
raise RuntimeError("TAVILY_API_KEY not found in Hugging Face Secrets")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
llm = ChatGroq(
|
| 15 |
model_name="openai/gpt-oss-120b",
|
| 16 |
temperature=0,
|
|
@@ -22,31 +26,90 @@ search = TavilySearch(
|
|
| 22 |
tavily_api_key=tavily_api_key
|
| 23 |
)
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
def search_agent(query):
|
| 26 |
if not query.strip():
|
| 27 |
-
return "Please enter a valid
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
prompt = f"""
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
| 38 |
-
Answer clearly and concisely.
|
| 39 |
-
"""
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
demo.launch()
|
|
|
|
| 3 |
from langchain_groq import ChatGroq
|
| 4 |
from langchain_tavily import TavilySearch
|
| 5 |
|
| 6 |
+
# ===============================
|
| 7 |
+
# Load API Keys from HF Secrets
|
| 8 |
+
# ===============================
|
| 9 |
groq_api_key = os.environ.get("GROQ_API_KEY")
|
| 10 |
tavily_api_key = os.environ.get("TAVILY_API_KEY")
|
| 11 |
|
| 12 |
+
if not groq_api_key or not tavily_api_key:
|
| 13 |
+
raise ValueError("❌ API keys not found. Please add them in Hugging Face Secrets.")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# ===============================
|
| 16 |
+
# Initialize LLM & Search Tool
|
| 17 |
+
# ===============================
|
| 18 |
llm = ChatGroq(
|
| 19 |
model_name="openai/gpt-oss-120b",
|
| 20 |
temperature=0,
|
|
|
|
| 26 |
tavily_api_key=tavily_api_key
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# ===============================
|
| 30 |
+
# Search Agent Function
|
| 31 |
+
# ===============================
|
| 32 |
def search_agent(query):
|
| 33 |
if not query.strip():
|
| 34 |
+
return "⚠️ Please enter a valid query."
|
| 35 |
|
| 36 |
+
results = search.invoke(query)
|
| 37 |
+
contexts = results.get("results", [])
|
| 38 |
+
|
| 39 |
+
if not contexts:
|
| 40 |
+
return "❌ No relevant information found."
|
| 41 |
+
|
| 42 |
+
context_text = "\n".join([r["content"] for r in contexts])
|
| 43 |
|
| 44 |
prompt = f"""
|
| 45 |
+
Using the following information from web search:
|
| 46 |
+
|
| 47 |
+
{context_text}
|
| 48 |
+
|
| 49 |
+
Question: {query}
|
| 50 |
+
Answer clearly and concisely:
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
response = llm.invoke(prompt)
|
| 54 |
+
return response.content.strip()
|
| 55 |
+
|
| 56 |
+
custom_css = """
|
| 57 |
+
body {
|
| 58 |
+
background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
.gradio-container {
|
| 62 |
+
max-width: 900px !important;
|
| 63 |
+
margin: auto;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
#title {
|
| 67 |
+
font-size: 36px;
|
| 68 |
+
font-weight: 700;
|
| 69 |
+
text-align: center;
|
| 70 |
+
color: white;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
#subtitle {
|
| 74 |
+
text-align: center;
|
| 75 |
+
color: #cfd8dc;
|
| 76 |
+
margin-bottom: 20px;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
.card {
|
| 80 |
+
background: #111827;
|
| 81 |
+
border-radius: 16px;
|
| 82 |
+
padding: 24px;
|
| 83 |
+
box-shadow: 0px 10px 30px rgba(0,0,0,0.4);
|
| 84 |
+
}
|
| 85 |
+
"""
|
| 86 |
|
| 87 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 88 |
|
| 89 |
+
gr.Markdown("<div id='title'>Nithi's Search Assistant</div>")
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
with gr.Column(elem_classes="card"):
|
| 92 |
+
query_input = gr.Textbox(
|
| 93 |
+
label="Enter your question",
|
| 94 |
+
placeholder="Enter your Question",
|
| 95 |
+
lines=2
|
| 96 |
+
)
|
| 97 |
|
| 98 |
+
search_btn = gr.Button(
|
| 99 |
+
"Search",
|
| 100 |
+
variant="primary"
|
| 101 |
+
)
|
| 102 |
|
| 103 |
+
output_box = gr.Textbox(
|
| 104 |
+
label="AI Answer",
|
| 105 |
+
lines=10,
|
| 106 |
+
interactive=False
|
| 107 |
+
)
|
| 108 |
|
| 109 |
+
search_btn.click(
|
| 110 |
+
fn=search_agent,
|
| 111 |
+
inputs=query_input,
|
| 112 |
+
outputs=output_box
|
| 113 |
+
)
|
| 114 |
|
| 115 |
demo.launch()
|