Wayne0102 commited on
Commit
83df412
·
verified ·
1 Parent(s): 233c616

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -19
app.py CHANGED
@@ -9,20 +9,17 @@ from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
9
  # 1. SETUP LLM
10
  hf_token = os.getenv("HF_TOKEN")
11
 
 
12
  llm = HuggingFaceInferenceAPI(
13
- # Switching to 7B as it is more stable on the free API
14
- model_name="Qwen/Qwen2.5-Coder-7B-Instruct",
15
  token=hf_token,
16
- # Fix for the 404 error: explicitly use text-generation task
17
- task="text-generation",
18
- # Use together or auto to ensure the model is found
19
- provider="together",
20
  is_function_calling_model=False
21
  )
22
 
23
- # 2. DEFINE TOOLS
24
  def get_tokyo_time() -> str:
25
- """Useful for when you need to know the current time in Tokyo, Japan."""
26
  tz = pytz.timezone('Asia/Tokyo')
27
  return f"The current time in Tokyo is {datetime.datetime.now(tz).strftime('%H:%M:%S')}"
28
 
@@ -36,7 +33,7 @@ tools = [
36
  ]
37
 
38
  # 3. CREATE THE AGENT
39
- # Ensure you are importing ReActAgent from llama_index.core.agent
40
  agent = ReActAgent.from_tools(
41
  tools,
42
  llm=llm,
@@ -46,17 +43,11 @@ agent = ReActAgent.from_tools(
46
  # 4. GRADIO INTERFACE
47
  def chat(message, history):
48
  try:
 
49
  response = agent.chat(message)
50
  return str(response)
51
  except Exception as e:
52
- # Better error handling for the UI
53
- return f"Agent Error: {str(e)}"
54
 
55
- demo = gr.ChatInterface(
56
- fn=chat,
57
- title="Unit 2: LlamaIndex Agent",
58
- description="I can tell you the time in Tokyo or multiply numbers!"
59
- )
60
-
61
- if __name__ == "__main__":
62
- demo.launch()
 
9
  # 1. SETUP LLM
10
  hf_token = os.getenv("HF_TOKEN")
11
 
12
+ # We use the 7B model because it's faster and more available on the free tier
13
  llm = HuggingFaceInferenceAPI(
14
+ model_name="Qwen/Qwen2.5-7B-Instruct",
 
15
  token=hf_token,
16
+ task="text-generation",
 
 
 
17
  is_function_calling_model=False
18
  )
19
 
20
+ # 2. DEFINE YOUR TOOLS
21
  def get_tokyo_time() -> str:
22
+ """Returns the current time in Tokyo, Japan."""
23
  tz = pytz.timezone('Asia/Tokyo')
24
  return f"The current time in Tokyo is {datetime.datetime.now(tz).strftime('%H:%M:%S')}"
25
 
 
33
  ]
34
 
35
  # 3. CREATE THE AGENT
36
+ # The system prompt ensures the AI follows the ReAct pattern
37
  agent = ReActAgent.from_tools(
38
  tools,
39
  llm=llm,
 
43
  # 4. GRADIO INTERFACE
44
  def chat(message, history):
45
  try:
46
+ # Use .chat() to maintain memory
47
  response = agent.chat(message)
48
  return str(response)
49
  except Exception as e:
50
+ # This will show you exactly what is failing in the Gradio UI
51
+ return f"Error: {str(e)}"
52
 
53
+ gr.ChatInterface(chat, title="Unit 2: LlamaIndex Agent").launch()