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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
from typing import TypedDict
|
| 5 |
+
from langgraph.graph import StateGraph, START, END
|
| 6 |
+
from langchain_groq import ChatGroq
|
| 7 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
+
# 1. Config β set your key here or via HF Secrets
|
| 12 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
+
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
|
| 14 |
+
|
| 15 |
+
llm = ChatGroq(model="llama-3.1-8b-instant")
|
| 16 |
+
search = DuckDuckGoSearchRun()
|
| 17 |
+
|
| 18 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
+
# 2. State + Graph (same as research_agent.py)
|
| 20 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
+
class ResearchState(TypedDict):
|
| 22 |
+
query: str
|
| 23 |
+
search_results: str
|
| 24 |
+
summary: str
|
| 25 |
+
|
| 26 |
+
def search_node(state: ResearchState):
|
| 27 |
+
return {"search_results": search.run(state["query"])}
|
| 28 |
+
|
| 29 |
+
def summarize_node(state: ResearchState):
|
| 30 |
+
prompt = f"""You are a research assistant. Answer the user's question clearly
|
| 31 |
+
based on the search results below. Be factual and concise (3-5 sentences).
|
| 32 |
+
|
| 33 |
+
Question: {state["query"]}
|
| 34 |
+
|
| 35 |
+
Search Results:
|
| 36 |
+
{state["search_results"]}"""
|
| 37 |
+
response = llm.invoke(prompt)
|
| 38 |
+
return {"summary": response.content}
|
| 39 |
+
|
| 40 |
+
graph = StateGraph(ResearchState)
|
| 41 |
+
graph.add_node("search", search_node)
|
| 42 |
+
graph.add_node("summarize", summarize_node)
|
| 43 |
+
graph.add_edge(START, "search")
|
| 44 |
+
graph.add_edge("search", "summarize")
|
| 45 |
+
graph.add_edge("summarize", END)
|
| 46 |
+
agent = graph.compile()
|
| 47 |
+
|
| 48 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
+
# 3. Gradio handler
|
| 50 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 51 |
+
def run_agent(query):
|
| 52 |
+
if not query.strip():
|
| 53 |
+
return "", "β οΈ Please enter a question."
|
| 54 |
+
|
| 55 |
+
start = time.time()
|
| 56 |
+
result = agent.invoke({"query": query})
|
| 57 |
+
elapsed = round(time.time() - start, 1)
|
| 58 |
+
|
| 59 |
+
answer = result["summary"]
|
| 60 |
+
status = f"β
Done in {elapsed}s Β· LangGraph β DuckDuckGo β ChatGroq"
|
| 61 |
+
return answer, status
|
| 62 |
+
|
| 63 |
+
def clear_all():
|
| 64 |
+
return "", "", "Ready."
|
| 65 |
+
|
| 66 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
# 4. UI
|
| 68 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 69 |
+
EXAMPLES = [
|
| 70 |
+
"What is LangGraph and how does it work?",
|
| 71 |
+
"Latest AI language models released in 2025?",
|
| 72 |
+
"How does Retrieval-Augmented Generation (RAG) work?",
|
| 73 |
+
"What is the difference between LangChain and LangGraph?",
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AI Research Agent") as demo:
|
| 77 |
+
|
| 78 |
+
gr.Markdown("""
|
| 79 |
+
## π AI Research Agent
|
| 80 |
+
Ask any question. The agent searches the web and returns a clear, summarized answer.
|
| 81 |
+
""")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
# ββ Left panel: Input ββ
|
| 85 |
+
with gr.Column(scale=2):
|
| 86 |
+
query_input = gr.Textbox(
|
| 87 |
+
label="Your Question",
|
| 88 |
+
placeholder="e.g. What is LangGraph?",
|
| 89 |
+
lines=3,
|
| 90 |
+
)
|
| 91 |
+
with gr.Row():
|
| 92 |
+
submit_btn = gr.Button("π Search", variant="primary", scale=3)
|
| 93 |
+
clear_btn = gr.Button("Clear", scale=1)
|
| 94 |
+
|
| 95 |
+
gr.Markdown("**Try an example:**")
|
| 96 |
+
for example in EXAMPLES:
|
| 97 |
+
gr.Button(example, size="sm").click(
|
| 98 |
+
fn=lambda e=example: e,
|
| 99 |
+
outputs=query_input
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# ββ Right panel: Output ββ
|
| 103 |
+
with gr.Column(scale=3):
|
| 104 |
+
output_box = gr.Textbox(
|
| 105 |
+
label="π Answer",
|
| 106 |
+
lines=12,
|
| 107 |
+
interactive=False,
|
| 108 |
+
)
|
| 109 |
+
status_md = gr.Markdown("Ready.")
|
| 110 |
+
|
| 111 |
+
# ββ Wire up events ββ
|
| 112 |
+
submit_btn.click(
|
| 113 |
+
fn=run_agent,
|
| 114 |
+
inputs=query_input,
|
| 115 |
+
outputs=[output_box, status_md],
|
| 116 |
+
)
|
| 117 |
+
query_input.submit( # also fires on Enter key
|
| 118 |
+
fn=run_agent,
|
| 119 |
+
inputs=query_input,
|
| 120 |
+
outputs=[output_box, status_md],
|
| 121 |
+
)
|
| 122 |
+
clear_btn.click(
|
| 123 |
+
fn=clear_all,
|
| 124 |
+
outputs=[query_input, output_box, status_md],
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
demo.launch()# app.py
|
| 128 |
+
import os
|
| 129 |
+
import time
|
| 130 |
+
from typing import TypedDict
|
| 131 |
+
from langgraph.graph import StateGraph, START, END
|
| 132 |
+
from langchain_groq import ChatGroq
|
| 133 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 134 |
+
import gradio as gr
|
| 135 |
+
|
| 136 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 137 |
+
# 1. Config β set your key here or via HF Secrets
|
| 138 |
+
# βββββββββββββββββοΏ½οΏ½οΏ½βββββββββββββββββββββββ
|
| 139 |
+
os.environ["GROQ_API_KEY"] = "your_groq_api_key_here"
|
| 140 |
+
|
| 141 |
+
llm = ChatGroq(model="llama3-8b-8192", temperature=0.3)
|
| 142 |
+
search = DuckDuckGoSearchRun()
|
| 143 |
+
|
| 144 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 145 |
+
# 2. State + Graph (same as research_agent.py)
|
| 146 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 147 |
+
class ResearchState(TypedDict):
|
| 148 |
+
query: str
|
| 149 |
+
search_results: str
|
| 150 |
+
summary: str
|
| 151 |
+
|
| 152 |
+
def search_node(state: ResearchState):
|
| 153 |
+
return {"search_results": search.run(state["query"])}
|
| 154 |
+
|
| 155 |
+
def summarize_node(state: ResearchState):
|
| 156 |
+
prompt = f"""You are a research assistant. Answer the user's question clearly
|
| 157 |
+
based on the search results below. Be factual and concise (3-5 sentences).
|
| 158 |
+
|
| 159 |
+
Question: {state["query"]}
|
| 160 |
+
|
| 161 |
+
Search Results:
|
| 162 |
+
{state["search_results"]}"""
|
| 163 |
+
response = llm.invoke(prompt)
|
| 164 |
+
return {"summary": response.content}
|
| 165 |
+
|
| 166 |
+
graph = StateGraph(ResearchState)
|
| 167 |
+
graph.add_node("search", search_node)
|
| 168 |
+
graph.add_node("summarize", summarize_node)
|
| 169 |
+
graph.add_edge(START, "search")
|
| 170 |
+
graph.add_edge("search", "summarize")
|
| 171 |
+
graph.add_edge("summarize", END)
|
| 172 |
+
agent = graph.compile()
|
| 173 |
+
|
| 174 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 175 |
+
# 3. Gradio handler
|
| 176 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 177 |
+
def run_agent(query):
|
| 178 |
+
if not query.strip():
|
| 179 |
+
return "", "β οΈ Please enter a question."
|
| 180 |
+
|
| 181 |
+
start = time.time()
|
| 182 |
+
result = agent.invoke({"query": query})
|
| 183 |
+
elapsed = round(time.time() - start, 1)
|
| 184 |
+
|
| 185 |
+
answer = result["summary"]
|
| 186 |
+
status = f"β
Done in {elapsed}s Β· LangGraph β DuckDuckGo β ChatGroq"
|
| 187 |
+
return answer, status
|
| 188 |
+
|
| 189 |
+
def clear_all():
|
| 190 |
+
return "", "", "Ready."
|
| 191 |
+
|
| 192 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 193 |
+
# 4. UI
|
| 194 |
+
# βββββββββββββββββββββββββββββββββββββββββ
|
| 195 |
+
EXAMPLES = [
|
| 196 |
+
"What is LangGraph and how does it work?",
|
| 197 |
+
"Latest AI language models released in 2025?",
|
| 198 |
+
"How does Retrieval-Augmented Generation (RAG) work?",
|
| 199 |
+
"What is the difference between LangChain and LangGraph?",
|
| 200 |
+
]
|
| 201 |
+
|
| 202 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AI Research Agent") as demo:
|
| 203 |
+
|
| 204 |
+
gr.Markdown("""
|
| 205 |
+
## π AI Research Agent
|
| 206 |
+
Ask any question. The agent searches the web and returns a clear, summarized answer.
|
| 207 |
+
""")
|
| 208 |
+
|
| 209 |
+
with gr.Row():
|
| 210 |
+
# ββ Left panel: Input ββ
|
| 211 |
+
with gr.Column(scale=2):
|
| 212 |
+
query_input = gr.Textbox(
|
| 213 |
+
label="Your Question",
|
| 214 |
+
placeholder="e.g. What is LangGraph?",
|
| 215 |
+
lines=3,
|
| 216 |
+
)
|
| 217 |
+
with gr.Row():
|
| 218 |
+
submit_btn = gr.Button("π Search", variant="primary", scale=3)
|
| 219 |
+
clear_btn = gr.Button("Clear", scale=1)
|
| 220 |
+
|
| 221 |
+
gr.Markdown("**Try an example:**")
|
| 222 |
+
for example in EXAMPLES:
|
| 223 |
+
gr.Button(example, size="sm").click(
|
| 224 |
+
fn=lambda e=example: e,
|
| 225 |
+
outputs=query_input
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
# ββ Right panel: Output ββ
|
| 229 |
+
with gr.Column(scale=3):
|
| 230 |
+
output_box = gr.Textbox(
|
| 231 |
+
label="π Answer",
|
| 232 |
+
lines=12,
|
| 233 |
+
interactive=False,
|
| 234 |
+
)
|
| 235 |
+
status_md = gr.Markdown("Ready.")
|
| 236 |
+
|
| 237 |
+
# ββ Wire up events ββ
|
| 238 |
+
submit_btn.click(
|
| 239 |
+
fn=run_agent,
|
| 240 |
+
inputs=query_input,
|
| 241 |
+
outputs=[output_box, status_md],
|
| 242 |
+
)
|
| 243 |
+
query_input.submit( # also fires on Enter key
|
| 244 |
+
fn=run_agent,
|
| 245 |
+
inputs=query_input,
|
| 246 |
+
outputs=[output_box, status_md],
|
| 247 |
+
)
|
| 248 |
+
clear_btn.click(
|
| 249 |
+
fn=clear_all,
|
| 250 |
+
outputs=[query_input, output_box, status_md],
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
demo.launch()
|