Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,56 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
-
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from langchain import hub
|
| 7 |
+
from langchain.agents import AgentExecutor, create_react_agent
|
| 8 |
+
from langchain_core.prompts import PromptTemplate
|
| 9 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 10 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 14 |
# --- Basic Agent Definition ---
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
+
# Prompt
|
| 17 |
+
template = '''Answer the following questions as best you can. You have access to the following tools:
|
| 18 |
+
|
| 19 |
+
{tools}
|
| 20 |
+
|
| 21 |
+
Use the following format:
|
| 22 |
+
|
| 23 |
+
Question: the input question you must answer
|
| 24 |
+
Thought: you should always think about what to do
|
| 25 |
+
Action: the action to take, should be one of [{tool_names}]
|
| 26 |
+
Action Input: the input to the action
|
| 27 |
+
Observation: the result of the action
|
| 28 |
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 29 |
+
Thought: I now know the final answer
|
| 30 |
+
Final Answer: the final answer to the original input question
|
| 31 |
+
|
| 32 |
+
Begin!
|
| 33 |
+
Question: {input}
|
| 34 |
+
Thought:{agent_scratchpad}'''
|
| 35 |
+
prompt = PromptTemplate.from_template(template)
|
| 36 |
+
|
| 37 |
+
# Tools
|
| 38 |
+
search_tool = DuckDuckGoSearchRun()
|
| 39 |
+
tools = [search_tool]
|
| 40 |
+
|
| 41 |
+
# LLM
|
| 42 |
+
model = HuggingFaceEndpoint(
|
| 43 |
+
repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 44 |
+
huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
class BasicAgent:
|
| 48 |
def __init__(self):
|
| 49 |
+
self.agent = create_react_agent(model, tools, prompt)
|
| 50 |
+
self.agent_executor = AgentExecutor(agent = agent, tools = tools)
|
| 51 |
def __call__(self, question: str) -> str:
|
| 52 |
+
response = self.agent_executor.invoke(
|
| 53 |
+
"input": question
|
| 54 |
+
)
|
| 55 |
+
return response
|
| 56 |
|
| 57 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 58 |
"""
|