Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,61 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
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 |
def __call__(self, question: str) -> str:
|
| 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 |
"""
|
|
|
|
| 1 |
+
import inspect
|
| 2 |
import os
|
| 3 |
+
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
+
import requests
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from langchain_community.chat_models import ChatHuggingFace
|
| 9 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
| 10 |
+
from langchain_core.messages import HumanMessage
|
| 11 |
+
|
| 12 |
+
from tools import (absolute, add, divide, exponential, floor_divide,
|
| 13 |
+
get_current_time_in_timezone, logarithm, modulus, multiply,
|
| 14 |
+
power, roman_calculator_converter, square_root, subtract,
|
| 15 |
+
web_search)
|
| 16 |
|
| 17 |
# (Keep Constants as is)
|
| 18 |
# --- Constants ---
|
| 19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 20 |
+
MAX_AGENT_ITERATIONS = 15
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
load_dotenv()
|
| 24 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 25 |
|
| 26 |
# --- Basic Agent Definition ---
|
| 27 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 28 |
class BasicAgent:
|
| 29 |
def __init__(self):
|
| 30 |
print("BasicAgent initialized.")
|
| 31 |
+
self.llm = HuggingFaceEndpoint(repo_id="Qwen/Qwen2.5-Coder-32B-Instruct", huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,)
|
| 32 |
+
self.chat = ChatHuggingFace(llm=llm, verbose=True)
|
| 33 |
+
self.tools = [
|
| 34 |
+
multiply,
|
| 35 |
+
add,
|
| 36 |
+
subtract,
|
| 37 |
+
power,
|
| 38 |
+
divide,
|
| 39 |
+
modulus,
|
| 40 |
+
square_root,
|
| 41 |
+
floor_divide,
|
| 42 |
+
absolute,
|
| 43 |
+
logarithm,
|
| 44 |
+
exponential,
|
| 45 |
+
web_search,
|
| 46 |
+
roman_calculator_converter,
|
| 47 |
+
get_current_time_in_timezone,
|
| 48 |
+
]
|
| 49 |
+
self.chat_with_tools = chat.bind_tools(tools)
|
| 50 |
+
print(f"Total tools available: {len(self.tools)}")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
def __call__(self, question: str) -> str:
|
| 54 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 55 |
+
messages = [HumanMessage(content=question)]
|
| 56 |
+
messages = self.chat_with_tools.invoke({"messages": messages})
|
| 57 |
+
answer = messages['messages'][-1].content
|
| 58 |
+
return answer[14:]
|
| 59 |
|
| 60 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 61 |
"""
|