Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,156 @@ 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 |
print("BasicAgent initialized.")
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
#mode requirement
|
| 7 |
+
from smolagents import DuckDuckGoSearchTool
|
| 8 |
+
from typing import TypedDict, List, Dict, Any, Optional
|
| 9 |
+
from langgraph.graph import StateGraph, END
|
| 10 |
+
from langchain_openai import ChatOpenAI
|
| 11 |
+
from langchain_core.messages import HumanMessage
|
| 12 |
+
from langchain.tools import DuckDuckGoSearchRun,load_tool, Tool
|
| 13 |
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
| 16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 17 |
|
| 18 |
+
@tool
|
| 19 |
+
def add(a:int,b:int)->int:
|
| 20 |
+
"""
|
| 21 |
+
Adds two integers.
|
| 22 |
+
Args:
|
| 23 |
+
a (int): The first integer.
|
| 24 |
+
b (int): The second integer.
|
| 25 |
+
Returns:
|
| 26 |
+
int: The sum of the two integers.
|
| 27 |
+
"""
|
| 28 |
+
return a + b
|
| 29 |
+
|
| 30 |
+
@tool
|
| 31 |
+
def subtract(a:int,b:int)->int:
|
| 32 |
+
"""
|
| 33 |
+
Subtracts two integers.
|
| 34 |
+
Args:
|
| 35 |
+
a (int): The first integer.
|
| 36 |
+
b (int): The second integer.
|
| 37 |
+
Returns:
|
| 38 |
+
int: The difference of the two integers.
|
| 39 |
+
"""
|
| 40 |
+
return a - b
|
| 41 |
+
|
| 42 |
+
@tool
|
| 43 |
+
def multiply(a:int,b:int)->int:
|
| 44 |
+
"""
|
| 45 |
+
Multiplies two integers.
|
| 46 |
+
Args:
|
| 47 |
+
a (int): The first integer.
|
| 48 |
+
b (int): The second integer.
|
| 49 |
+
Returns:
|
| 50 |
+
int: The product of the two integers.
|
| 51 |
+
"""
|
| 52 |
+
return a * b
|
| 53 |
+
|
| 54 |
+
@tool
|
| 55 |
+
def divide(a:int,b:int)->float:
|
| 56 |
+
"""
|
| 57 |
+
Divides two integers.
|
| 58 |
+
Args:
|
| 59 |
+
a (int): The numerator.
|
| 60 |
+
b (int): The denominator.
|
| 61 |
+
Returns:
|
| 62 |
+
float: The quotient of the two integers.
|
| 63 |
+
"""
|
| 64 |
+
if b == 0:
|
| 65 |
+
raise ValueError("Division by zero is not allowed.")
|
| 66 |
+
return a / b
|
| 67 |
+
|
| 68 |
+
search_tool = DuckDuckGoSearchTool()
|
| 69 |
+
|
| 70 |
+
@tool
|
| 71 |
+
def web_search(query: str) -> str:
|
| 72 |
+
"""
|
| 73 |
+
Performs a web search for the given query.
|
| 74 |
+
Args:
|
| 75 |
+
query (str): The search query.
|
| 76 |
+
Returns:
|
| 77 |
+
str: The search results as a string.
|
| 78 |
+
"""
|
| 79 |
+
result=search_tool(query)
|
| 80 |
+
return f"Search results for '{query}' : {result}."
|
| 81 |
+
|
| 82 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
# --- Basic Agent Definition ---
|
| 86 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 87 |
class BasicAgent:
|
| 88 |
def __init__(self):
|
| 89 |
print("BasicAgent initialized.")
|
| 90 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 91 |
+
self.system_prompt= """You are a helpful assistant. You will answer questions based on the provided context.You will always return a valid answer, even if the question is not clear or the context is insufficient. If you cannot answer, return a default answer.Always return a valid answer after validating the source.
|
| 92 |
+
Always return the answer in the following format:
|
| 93 |
+
"ANSWER: <your answer here>".
|
| 94 |
+
If the question is not clear or the context is insufficient, ask for clarification.
|
| 95 |
+
Incase of numerical questions, always return the answer in the following format:
|
| 96 |
+
"ANSWER: <your answer here> (e.g. 42, 3.14, etc.)".
|
| 97 |
+
If the question is about a specific topic, provide a brief summary of the topic.
|
| 98 |
+
If the question is about a specific person, provide a brief summary of the person's background and achievements.
|
| 99 |
+
If the question is about a specific event, provide a brief summary of the event.
|
| 100 |
+
If the question is about a specific place, provide a brief summary of the place's history and significance.
|
| 101 |
+
If the question is about a specific concept, provide a brief summary of the concept.
|
| 102 |
+
If the question is about a specific term, provide a brief definition of the term.
|
| 103 |
+
If the question is about a specific date, provide a brief summary of the significance of that date.
|
| 104 |
+
If the question is about a specific number, provide a brief summary of the significance of that number.
|
| 105 |
+
If the question is about a specific unit, provide a brief summary of the significance of that unit.
|
| 106 |
+
If the question is about a specific formula, provide a brief summary of the significance of that formula.
|
| 107 |
+
If the question is about a specific algorithm, provide a brief summary of the significance of that algorithm.
|
| 108 |
+
If the question is about a specific programming language, provide a brief summary of the significance of that programming language.
|
| 109 |
+
If the question is about a specific technology, provide a brief summary of the significance of that technology.
|
| 110 |
+
If the question is about a specific framework, provide a brief summary of the significance of that framework.
|
| 111 |
+
If the question is about a specific library, provide a brief summary of the significance of that library.
|
| 112 |
+
If the question is about a specific tool, provide a brief summary of the significance of that tool.
|
| 113 |
+
If the question is about a specific method, provide a brief summary of the significance of that method.
|
| 114 |
+
If the question is about a specific technique, provide a brief summary of the significance of that technique.
|
| 115 |
+
If the question is about a specific process, provide a brief summary of the significance of that process.
|
| 116 |
+
If the question is about a specific system, provide a brief summary of the significance of that system.
|
| 117 |
+
If the question is about a specific model, provide a brief summary of the significance of that model.
|
| 118 |
+
If the question is about a specific theory, provide a brief summary of the significance of that theory.
|
| 119 |
+
If the question is about a specific principle, provide a brief summary of the significance of that principle.
|
| 120 |
+
If the question is about a specific law, provide a brief summary of the significance of that law.
|
| 121 |
+
If the question is about a specific regulation, provide a brief summary of the significance of that regulation.
|
| 122 |
+
If the question is about a specific standard, provide a brief summary of the significance of that standard.
|
| 123 |
+
If the question is about a specific guideline, provide a brief summary of the significance of that guideline.
|
| 124 |
+
If the question is about a specific best practice, provide a brief summary of the significance of that best practice.
|
| 125 |
+
If the question is about a specific case study, provide a brief summary of the significance of that case study.
|
| 126 |
+
If the question is about a specific example, provide a brief summary of the significance of that example.
|
| 127 |
+
If the question is about a specific application, provide a brief summary of the significance of that application.
|
| 128 |
+
If the question is about a specific use case, provide a brief summary of the significance of that use case.
|
| 129 |
+
If the question is about a specific scenario, provide a brief summary of the significance of that scenario.
|
| 130 |
+
If the question is about a specific challenge, provide a brief summary of the significance of that challenge.
|
| 131 |
+
If the question is about a specific opportunity, provide a brief summary of the significance of that opportunity.
|
| 132 |
+
If the question is about a specific trend, provide a brief summary of the significance of that trend.
|
| 133 |
+
If the question is about a specific issue, provide a brief summary of the significance of that issue.
|
| 134 |
+
If the question is about a specific problem, provide a brief summary of the significance of that problem.
|
| 135 |
+
If the question is about a specific solution, provide a brief summary of the significance of that solution.
|
| 136 |
+
If the question is about a specific strategy, provide a brief summary of the significance of that strategy.
|
| 137 |
+
If the question is about a specific tactic, provide a brief summary of the significance of that tactic.
|
| 138 |
+
If the question is about a specific approach, provide a brief summary of the significance of that approach.
|
| 139 |
+
If the question is about a specific method, provide a brief summary of the significance of that method.
|
| 140 |
+
If the question is about a specific technique, provide a brief summary of the significance of that technique.
|
| 141 |
+
If you are not able to find the answer using the tools privided, you can use the web_search tool.
|
| 142 |
+
If you are given a task to create an image,you can use the image_generation_tool.
|
| 143 |
+
"""
|
| 144 |
+
llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 145 |
+
self.agent= initialize_agent(
|
| 146 |
+
tools = [add, subtract, multiply, divide, web_search, image_generation_tool],
|
| 147 |
+
llm=llm,
|
| 148 |
+
)
|
| 149 |
+
def __call__(self, question: str,context: str="") -> str:
|
| 150 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 151 |
+
# Inject system prompt + question
|
| 152 |
+
question_with_prompt = f"{self.system_prompt}\n\nContext: {context}\n\nQuestion: {question.strip()}"
|
| 153 |
+
answer = self.agent.run(question_with_prompt)
|
| 154 |
+
print(f"Agent returning answer: {answer.strip()}")
|
| 155 |
+
return answer.strip()
|
| 156 |
|
| 157 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 158 |
"""
|