naman1102 commited on
Commit
8cf3cc0
·
1 Parent(s): 1d20af3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -21
app.py CHANGED
@@ -7,11 +7,12 @@ from typing import Dict, Any, List, TypedDict, Optional
7
  from langgraph.graph import Graph, StateGraph
8
  from langgraph.prebuilt import ToolNode
9
  from tools import simple_search
 
 
10
  print("trial")
11
  # (Keep Constants as is)
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
- MODEL_API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-7B-Instruct"
15
  HF_TOKEN = os.getenv("HF_TOKEN") # Make sure to set this environment variable
16
 
17
  class AgentState(TypedDict):
@@ -25,21 +26,30 @@ class AgentState(TypedDict):
25
 
26
  class BasicAgent:
27
  def __init__(self):
28
- print("Initializing BasicAgent with Qwen2.5-Coder-32B-Instruct API...")
29
  if not HF_TOKEN:
30
  raise ValueError("HF_TOKEN environment variable not set. Please set your Hugging Face API token.")
31
 
32
- # Set up headers for API calls
33
- self.headers = {
34
- "Authorization": f"Bearer {HF_TOKEN}",
35
- "Content-Type": "application/json"
36
- }
37
 
38
  # Create the agent workflow
39
  print("Creating workflow variable")
40
  self.workflow = self._create_workflow()
41
  print("BasicAgent initialization complete.")
42
 
 
 
 
 
 
 
 
 
 
43
  def _create_workflow(self) -> Graph:
44
  """Create the agent workflow using LangGraph."""
45
  # Create the workflow with state schema
@@ -79,20 +89,6 @@ class BasicAgent:
79
 
80
  return workflow.compile()
81
 
82
- def _call_llm_api(self, prompt: str) -> str:
83
- """Call the Qwen model through the Hugging Face API."""
84
- try:
85
- response = requests.post(
86
- MODEL_API_URL,
87
- headers=self.headers,
88
- json={"inputs": prompt, "parameters": {"max_length": 200}}
89
- )
90
- response.raise_for_status()
91
- return response.json()[0]["generated_text"]
92
- except Exception as e:
93
- print(f"Error calling LLM API: {e}")
94
- return f"Error getting response from LLM: {str(e)}"
95
-
96
  def _analyze_question(self, state: AgentState) -> AgentState:
97
  """Analyze the question and determine the next step."""
98
  prompt = f"""Analyze this question and determine what needs to be done: {state['question']}
 
7
  from langgraph.graph import Graph, StateGraph
8
  from langgraph.prebuilt import ToolNode
9
  from tools import simple_search
10
+ from langchain_community.llms import HuggingFaceEndpoint
11
+
12
  print("trial")
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
16
  HF_TOKEN = os.getenv("HF_TOKEN") # Make sure to set this environment variable
17
 
18
  class AgentState(TypedDict):
 
26
 
27
  class BasicAgent:
28
  def __init__(self):
29
+ print("Initializing BasicAgent with Qwen2.5-Coder-7B-Instruct...")
30
  if not HF_TOKEN:
31
  raise ValueError("HF_TOKEN environment variable not set. Please set your Hugging Face API token.")
32
 
33
+ # Initialize LLM
34
+ self.llm = HuggingFaceEndpoint(
35
+ repo_id="Qwen/Qwen2.5-Coder-7B-Instruct",
36
+ huggingfacehub_api_token=HF_TOKEN
37
+ )
38
 
39
  # Create the agent workflow
40
  print("Creating workflow variable")
41
  self.workflow = self._create_workflow()
42
  print("BasicAgent initialization complete.")
43
 
44
+ def _call_llm_api(self, prompt: str) -> str:
45
+ """Call the Qwen model through the Hugging Face API."""
46
+ try:
47
+ response = self.llm.invoke(prompt)
48
+ return response
49
+ except Exception as e:
50
+ print(f"Error calling LLM API: {e}")
51
+ return f"Error getting response from LLM: {str(e)}"
52
+
53
  def _create_workflow(self) -> Graph:
54
  """Create the agent workflow using LangGraph."""
55
  # Create the workflow with state schema
 
89
 
90
  return workflow.compile()
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  def _analyze_question(self, state: AgentState) -> AgentState:
93
  """Analyze the question and determine the next step."""
94
  prompt = f"""Analyze this question and determine what needs to be done: {state['question']}