added llm
Browse files123456789101112131415161718192021222367 unmodified lines91929394959697import osimport gradio as grimport requestsimport inspectimport pandas as pd# (Keep Constants as is)# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent:def __init__(self):print("BasicAgent initialized.")def __call__(self, question: str) -> str:print(f"Agent received question (first 50 chars): {question[:50]}...")fixed_answer = "This is a default answer."print(f"Agent returning fixed answer: {fixed_answer}")return fixed_answerdef run_and_submit_all( profile: gr.OAuthProfile | None):"""67 unmodified linesprint("Agent did not produce any answers to submit.")return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)# 4. Prepare Submissionsubmission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."print(status_update)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014167 unmodified lines209210211212213214215import osimport gradio as grimport requestsimport pandas as pdfrom typing import TypedDictfrom langgraph.graph import StateGraph, END# (Keep Constants as is)# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"# Using a free Hugging Face model via Inference API (no auth required for public models)HF_API_URL = "https://api-inference.huggingface.co/models"HF_MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.2"# --- State Definition for LangGraph ---class AgentState(TypedDict):question: stranswer: str# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent:def __init__(self):print("BasicAgent initialized with LangGraph and Hugging Face API.")self.api_url = f"{HF_API_URL}/{HF_MODEL_ID}"print(f"Using Hugging Face Inference API: {self.api_url}")# Build LangGraph workflowself.workflow = self._build_graph()def _build_graph(self):"""Build the LangGraph workflow for answering questions."""workflow = StateGraph(AgentState)# Add node: process questionworkflow.add_node("process_question", self._process_question)# Set entry pointworkflow.set_entry_point("process_question")# Connect to endworkflow.add_edge("process_question", END)# Compile the graphreturn workflow.compile()def _call_hf_api(self, prompt: str) -> str:"""Call Hugging Face Inference API directly (free, no auth required for public models)."""try:# Format prompt for Mistral instruct modelformatted_prompt = f"<s>[INST] You are a helpful AI assistant. Answer the question concisely and accurately. Provide only the answer without any additional text like 'FINAL ANSWER' or explanations.\n\nQuestion: {prompt} [/INST]"payload = {"inputs": formatted_prompt,"parameters": {"max_new_tokens": 512,"temperature": 0.7,"return_full_text": False}}response = requests.post(self.api_url,json=payload,headers={"Content-Type": "application/json"},timeout=30)if response.status_code == 200:result = response.json()# Extract generated text from response (HF API returns list with dict)generated_text = ""if isinstance(result, list) and len(result) > 0:if isinstance(result[0], dict):generated_text = result[0].get("generated_text", "")else:generated_text = str(result[0])elif isinstance(result, dict):generated_text = result.get("generated_text", result.get("text", ""))else:generated_text = str(result)# Clean the responseanswer = generated_text.strip()# Remove "FINAL ANSWER:" if present (as per requirements)answer_upper = answer.upper()if "FINAL ANSWER:" in answer_upper:parts = answer.split("FINAL ANSWER:", 1)if len(parts) > 1:answer = parts[1].strip()elif "FINAL ANSWER" in answer_upper:parts = answer.split("FINAL ANSWER", 1)if len(parts) > 1:answer = parts[1].strip()return answerelif response.status_code == 503:# Model is loading, wait and retry onceerror_msg = "Model is loading, please try again in a moment."print(f"Warning: {error_msg}")return error_msgelse:error_msg = f"API returned status {response.status_code}: {response.text[:200]}"print(f"Error: {error_msg}")return f"Error: {error_msg}"except requests.exceptions.Timeout:return "Error: Request to Hugging Face API timed out."except requests.exceptions.RequestException as e:return f"Error: Failed to connect to Hugging Face API - {str(e)}"except Exception as e:return f"Error: Unexpected error - {str(e)}"def _process_question(self, state: AgentState) -> AgentState:"""Process the question and generate an answer using Hugging Face API."""question = state["question"]print(f"Processing question: {question[:100]}...")# Call Hugging Face APIanswer = self._call_hf_api(question)print(f"Generated answer (first 100 chars): {answer[:100]}...")return {"question": question, "answer": answer}def __call__(self, question: str) -> str:"""Main entry point for the agent."""print(f"Agent received question (first 50 chars): {question[:50]}...")# Run the LangGraph workflowtry:initial_state = {"question": question, "answer": ""}result = self.workflow.invoke(initial_state)answer = result.get("answer", "No answer generated.")print(f"Agent returning answer: {answer[:100]}...")return answerexcept Exception as e:print(f"Error in agent workflow: {e}")return f"Error processing question: {str(e)}"def run_and_submit_all( profile: gr.OAuthProfile | None):"""67 unmodified linesprint("Agent did not produce any answers to submit.")return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)# 4. Prepare Submissionsubmission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."print(status_update)
app.py+124-6