Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,64 @@
|
|
| 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 ---
|
|
@@ -13,11 +69,13 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 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 os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import inspect
|
| 6 |
import pandas as pd
|
| 7 |
+
from langchain_core.messages import HumanMessage
|
| 8 |
+
from langchain_core.messages import SystemMessage
|
| 9 |
+
from langchain_openai import ChatOpenAI
|
| 10 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
| 11 |
+
from langgraph.prebuilt import tools_condition, ToolNode
|
| 12 |
+
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
def convert_currency(amount: float, from_currency: str, to_currency: str) -> float:
|
| 16 |
+
"""Convert an amount from one currency to another using fixed exchange rates.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
amount: monetary amount to convert
|
| 20 |
+
from_currency: source currency code (e.g., "USD")
|
| 21 |
+
to_currency: target currency code (e.g., "CLP")
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
The converted amount rounded to two decimal places
|
| 25 |
+
"""
|
| 26 |
+
exchange_rates = {
|
| 27 |
+
('USD', 'EUR'): 0.91,
|
| 28 |
+
('EUR', 'USD'): 1.10,
|
| 29 |
+
('USD', 'CLP'): 920.0,
|
| 30 |
+
('CLP', 'USD'): 1 / 920.0,
|
| 31 |
+
('EUR', 'CLP'): 1010.0,
|
| 32 |
+
('CLP', 'EUR'): 1 / 1010.0,
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
rate = exchange_rates.get((from_currency.upper(), to_currency.upper()))
|
| 36 |
+
if rate is None:
|
| 37 |
+
raise ValueError(f"No exchange rate for {from_currency} → {to_currency}")
|
| 38 |
+
return round(amount * rate, 2)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
tools = [convert_currency]
|
| 42 |
+
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
|
| 43 |
+
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=False)
|
| 44 |
+
|
| 45 |
+
# System message
|
| 46 |
+
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing money exchange convert calculations")
|
| 47 |
+
|
| 48 |
+
# Node
|
| 49 |
+
def assistant(state: MessagesState):
|
| 50 |
+
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
|
| 51 |
+
|
| 52 |
+
# Build graph
|
| 53 |
+
builder = StateGraph(MessagesState)
|
| 54 |
+
builder.add_node("assistant", assistant)
|
| 55 |
+
builder.add_node("tools", ToolNode(tools))
|
| 56 |
+
builder.add_edge(START, "assistant")
|
| 57 |
+
builder.add_conditional_edges(
|
| 58 |
+
"assistant",
|
| 59 |
+
tools_condition,
|
| 60 |
+
)
|
| 61 |
+
builder.add_edge("tools", "assistant")
|
| 62 |
|
| 63 |
# (Keep Constants as is)
|
| 64 |
# --- Constants ---
|
|
|
|
| 69 |
class BasicAgent:
|
| 70 |
def __init__(self):
|
| 71 |
print("BasicAgent initialized.")
|
| 72 |
+
self.graph = builder.compile()
|
| 73 |
def __call__(self, question: str) -> str:
|
| 74 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 75 |
+
messages = [HumanMessage(content=question)]
|
| 76 |
+
result = self.graph.invoke({"messages": messages})
|
| 77 |
+
answer = result['messages'][-1].content
|
| 78 |
+
return answer
|
| 79 |
|
| 80 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 81 |
"""
|