Update app.py
Browse files
app.py
CHANGED
|
@@ -1,157 +1,128 @@
|
|
| 1 |
-
"""
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
-
results_df = pd.DataFrame(results_log)
|
| 90 |
-
return final_status, results_df
|
| 91 |
-
except requests.exceptions.HTTPError as e:
|
| 92 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 93 |
-
try:
|
| 94 |
-
error_json = e.response.json()
|
| 95 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 96 |
-
except Exception:
|
| 97 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 98 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 99 |
-
results_df = pd.DataFrame(results_log)
|
| 100 |
-
return status_message, results_df
|
| 101 |
-
except requests.exceptions.Timeout:
|
| 102 |
-
status_message = "Submission Failed: The request timed out."
|
| 103 |
-
results_df = pd.DataFrame(results_log)
|
| 104 |
-
return status_message, results_df
|
| 105 |
-
except requests.exceptions.RequestException as e:
|
| 106 |
-
status_message = f"Submission Failed: Network error - {e}"
|
| 107 |
-
results_df = pd.DataFrame(results_log)
|
| 108 |
-
return status_message, results_df
|
| 109 |
-
except Exception as e:
|
| 110 |
-
status_message = f"An unexpected error occurred during submission: {e}"
|
| 111 |
-
results_df = pd.DataFrame(results_log)
|
| 112 |
-
return status_message, results_df
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
with gr.Blocks() as demo:
|
| 116 |
-
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 117 |
-
gr.Markdown(
|
| 118 |
-
"""
|
| 119 |
-
**Instructions:**
|
| 120 |
-
|
| 121 |
-
1. Clone this space and modify the code to define your agent.
|
| 122 |
-
2. Log in with Hugging Face account using the button below.
|
| 123 |
-
3. Click 'Run Evaluation & Submit All Answers' to evaluate your agent and submit answers.
|
| 124 |
-
|
| 125 |
-
---
|
| 126 |
-
**Note:**
|
| 127 |
-
This is a simple baseline. Consider improving with caching or async processing.
|
| 128 |
-
"""
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
gr.LoginButton()
|
| 132 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 133 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 134 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 135 |
-
|
| 136 |
-
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 137 |
-
|
| 138 |
-
if __name__ == "__main__":
|
| 139 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 140 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
| 141 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 142 |
|
| 143 |
-
if space_host_startup:
|
| 144 |
-
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 145 |
-
print(f" Runtime URL: https://{space_host_startup}.hf.space")
|
| 146 |
else:
|
| 147 |
-
|
| 148 |
|
| 149 |
-
|
| 150 |
-
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 151 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 152 |
-
else:
|
| 153 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?).")
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""LangGraph Agent (GROQ version without Supabase)"""
|
| 2 |
import os
|
| 3 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
| 4 |
+
from langgraph.prebuilt import tools_condition, ToolNode
|
| 5 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
+
from langchain_groq import ChatGroq
|
| 7 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
|
| 8 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 9 |
+
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 10 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 11 |
+
from langchain_core.tools import tool
|
| 12 |
+
|
| 13 |
+
@tool
|
| 14 |
+
def multiply(a: int, b: int) -> int:
|
| 15 |
+
"""Multiply two numbers."""
|
| 16 |
+
return a * b
|
| 17 |
+
|
| 18 |
+
@tool
|
| 19 |
+
def add(a: int, b: int) -> int:
|
| 20 |
+
"""Add two numbers."""
|
| 21 |
+
return a + b
|
| 22 |
+
|
| 23 |
+
@tool
|
| 24 |
+
def subtract(a: int, b: int) -> int:
|
| 25 |
+
"""Subtract second number from the first."""
|
| 26 |
+
return a - b
|
| 27 |
+
|
| 28 |
+
@tool
|
| 29 |
+
def divide(a: int, b: int) -> float:
|
| 30 |
+
"""Divide two numbers."""
|
| 31 |
+
if b == 0:
|
| 32 |
+
raise ValueError("Cannot divide by zero.")
|
| 33 |
+
return a / b
|
| 34 |
+
|
| 35 |
+
@tool
|
| 36 |
+
def modulus(a: int, b: int) -> int:
|
| 37 |
+
"""Get the modulus (remainder) of two numbers."""
|
| 38 |
+
return a % b
|
| 39 |
+
|
| 40 |
+
@tool
|
| 41 |
+
def wiki_search(query: str) -> dict:
|
| 42 |
+
"""Search Wikipedia for a query and return up to 2 results."""
|
| 43 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 44 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 45 |
+
[
|
| 46 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 47 |
+
for doc in search_docs
|
| 48 |
+
])
|
| 49 |
+
return {"wiki_results": formatted_search_docs}
|
| 50 |
+
|
| 51 |
+
@tool
|
| 52 |
+
def web_search(query: str) -> dict:
|
| 53 |
+
"""Search Tavily for a query and return up to 3 results."""
|
| 54 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 55 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 56 |
+
[
|
| 57 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 58 |
+
for doc in search_docs
|
| 59 |
+
])
|
| 60 |
+
return {"web_results": formatted_search_docs}
|
| 61 |
+
|
| 62 |
+
@tool
|
| 63 |
+
def arvix_search(query: str) -> dict:
|
| 64 |
+
"""Search Arxiv and return up to 3 truncated results."""
|
| 65 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 66 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 67 |
+
[
|
| 68 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 69 |
+
for doc in search_docs
|
| 70 |
+
])
|
| 71 |
+
return {"arvix_results": formatted_search_docs}
|
| 72 |
+
|
| 73 |
+
# Load system prompt
|
| 74 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 75 |
+
system_prompt = f.read()
|
| 76 |
+
|
| 77 |
+
sys_msg = SystemMessage(content=system_prompt)
|
| 78 |
+
|
| 79 |
+
tools = [
|
| 80 |
+
multiply, add, subtract, divide, modulus,
|
| 81 |
+
wiki_search, web_search, arvix_search
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
def build_graph(provider: str = "groq"):
|
| 85 |
+
"""Build the LangGraph agent using specified LLM provider."""
|
| 86 |
+
|
| 87 |
+
if provider == "google":
|
| 88 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 89 |
+
|
| 90 |
+
elif provider == "groq":
|
| 91 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 92 |
+
if not groq_api_key:
|
| 93 |
+
raise ValueError("GROQ_API_KEY environment variable not set.")
|
| 94 |
+
llm = ChatGroq(model="qwen-qwq-32b", temperature=0, api_key=groq_api_key)
|
| 95 |
+
|
| 96 |
+
elif provider == "huggingface":
|
| 97 |
+
llm = ChatHuggingFace(
|
| 98 |
+
llm=HuggingFaceEndpoint(
|
| 99 |
+
url="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf",
|
| 100 |
+
temperature=0,
|
| 101 |
+
)
|
| 102 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
|
|
|
|
|
|
|
|
|
| 104 |
else:
|
| 105 |
+
raise ValueError("Invalid provider. Choose 'google', 'groq' or 'huggingface'.")
|
| 106 |
|
| 107 |
+
llm_with_tools = llm.bind_tools(tools)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
def assistant(state: MessagesState):
|
| 110 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 111 |
+
|
| 112 |
+
builder = StateGraph(MessagesState)
|
| 113 |
+
builder.add_node("assistant", assistant)
|
| 114 |
+
builder.add_node("tools", ToolNode(tools))
|
| 115 |
+
builder.add_edge(START, "assistant")
|
| 116 |
+
builder.add_conditional_edges("assistant", tools_condition)
|
| 117 |
+
builder.add_edge("tools", "assistant")
|
| 118 |
+
|
| 119 |
+
return builder.compile()
|
| 120 |
+
|
| 121 |
+
# For testing purposes
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 124 |
+
graph = build_graph(provider="groq")
|
| 125 |
+
messages = [HumanMessage(content=question)]
|
| 126 |
+
result = graph.invoke({"messages": messages})
|
| 127 |
+
for msg in result["messages"]:
|
| 128 |
+
print(msg.content)
|