Spaces:
Sleeping
Sleeping
Update app.py
Browse files11 unmodified lines1213141516171 unmodified line1920212223247 unmodified lines3233343536373839404142434445464748495051525354555657585960616263646566109 unmodified lines17617717817918018118218311 unmodified linesfrom langgraph.graph import StateGraph, START, ENDfrom langgraph.graph.message import add_messagesfrom youtube_transcript_api import YouTubeTranscriptApiload_dotenv()1 unmodified line# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"# --- LangGraph Agent State ---class AgentState(TypedDict):messages: Annotated[list, add_messages]7 unmodified linesclass BasicAgent:def __init__(self):print("LangGraph Agent initialized.")groq_client = Groq(api_key=os.getenv("GROQ_API_KEY") # Set this in HF Space secrets)# Initialize search toolself.search = DuckDuckGoSearchRun()# Build the LangGraph workflowself.workflow = self._build_workflow()self.app = self.workflow.compile()class LLMWrapper:def invoke(self, messages):# Extract content from HumanMessageif isinstance(messages, list) and len(messages) > 0:content = messages[0].content if hasattr(messages[0], 'content') else str(messages[0])else:content = str(messages)# Call Groqresponse = groq_client.chat.completions.create(model="llama-3.1-8b-instant",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": content}],temperature=0.1)return response.choices[0].message.contentself.llm = LLMWrapper()# # Initialize the language model# self.llm = ChatOllama(109 unmodified linesresponse = self.llm.invoke([message])# Extract only the FINAL ANSWER part# full_response = response.contentfull_response = responsefinal_answer = full_responseif "FINAL ANSWER:" in full_response:11 unmodified lines12131415161718191 unmodified line212223242526272829303132333435363738394041424344454647484950517 unmodified lines596061626364656667686970109 unmodified lines18018118218318418518611 unmodified linesfrom langgraph.graph import StateGraph, START, ENDfrom langgraph.graph.message import add_messagesfrom youtube_transcript_api import YouTubeTranscriptApifrom langchain.schema import AIMessagefrom huggingface_hub import InferenceClientload_dotenv()1 unmodified line# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"class HFInferenceLLM:def __init__(self):# Uses HF token automatically from the logged-in user or HF Spaceself.client = InferenceClient(model="meta-llama/Meta-Llama-3-8B-Instruct",token=os.getenv("HF_TOKEN") # Optional, auto-detected in HF Spaces)def invoke(self, messages):# Extract content from messagesif isinstance(messages, list) and len(messages) > 0:content = messages[0].content if hasattr(messages[0], 'content') else str(messages[0])else:content = str(messages)# Generate responseresponse = self.client.text_generation(content,max_new_tokens=500,temperature=0.1,do_sample=True,)return AIMessage(content=response)# --- LangGraph Agent State ---class AgentState(TypedDict):messages: Annotated[list, add_messages]7 unmodified linesclass BasicAgent:def __init__(self):print("LangGraph Agent initialized.")self.llm = HFInferenceLLM()# Initialize search toolself.search = DuckDuckGoSearchRun()# Build the LangGraph workflowself.workflow = self._build_workflow()self.app = self.workflow.compile()# # Initialize the language model# self.llm = ChatOllama(109 unmodified linesresponse = self.llm.invoke([message])# Extract only the FINAL ANSWER partfull_response = response.contentfinal_answer = full_responseif "FINAL ANSWER:" in full_response:
app.py+29-26