Spaces:
Sleeping
Sleeping
Download app.py from Pranjali29/project2: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/spaces/Pranjali29/project2/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pranjali29/project2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pranjali29/project2/resolve/main/app.py
2.25 kB
| # ----------------------------- | |
| # 1. Imports | |
| # ----------------------------- | |
| from langchain.agents import create_agent | |
| from langchain_openai import ChatOpenAI | |
| from langchain_core.tools import tool | |
| from tavily import TavilyClient | |
| import os | |
| # ----------------------------- | |
| # 2. API Keys (IMPORTANT) | |
| # ----------------------------- | |
| OPENROUTER_API_KEY = "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" | |
| TAVILY_API_KEY = "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" | |
| # ----------------------------- | |
| # 3. LLM Setup (OpenRouter) | |
| # ----------------------------- | |
| llm = ChatOpenAI( | |
| model="meta-llama/llama-3-8b-instruct", | |
| openai_api_base="https://openrouter.ai/api/v1", | |
| openai_api_key=OPENROUTER_API_KEY, | |
| temperature=0.3 | |
| ) | |
| # ----------------------------- | |
| # 4. Tools | |
| # ----------------------------- | |
| tavily = TavilyClient(api_key=TAVILY_API_KEY) | |
| def search_tool(query: str) -> str: | |
| """Search the web for latest info.""" | |
| results = tavily.search(query=query, max_results=3) | |
| return str(results) | |
| def summarize_tool(text: str) -> str: | |
| """Summarize text into insights.""" | |
| prompt = f""" | |
| Summarize into: | |
| - Key Points | |
| - Risks | |
| - Opportunities | |
| {text} | |
| """ | |
| return llm.invoke(prompt).content | |
| # ----------------------------- | |
| # 5. Agent | |
| # ----------------------------- | |
| agent = create_agent( | |
| model=llm, | |
| tools=[search_tool, summarize_tool], | |
| system_prompt=""" | |
| You are an intelligent research assistant. | |
| Steps: | |
| 1. Search information | |
| 2. Summarize it | |
| 3. Give final structured answer | |
| """ | |
| ) | |
| # ----------------------------- | |
| # 6. Function for UI | |
| # ----------------------------- | |
| def chat(query): | |
| response = agent.invoke({ | |
| "messages": [ | |
| {"role": "user", "content": query} | |
| ] | |
| }) | |
| return response["messages"][-1].content | |
| import gradio as gr | |
| interface = gr.Interface( | |
| fn=chat, | |
| inputs=gr.Textbox(lines=2, placeholder="Ask something..."), | |
| outputs="text", | |
| title="AI Research Assistant", | |
| description="Search + Summarize AI Agent" | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() |