Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,176 +1,112 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import gradio as gr
|
| 4 |
-
from
|
| 5 |
-
from
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
return {"location": "Tokyo", "temperature": "10", "unit": "celsius"}
|
| 12 |
-
elif "san francisco" in location
|
| 13 |
return {"location": "San Francisco", "temperature": "72", "unit": "fahrenheit"}
|
| 14 |
-
elif "paris" in location
|
| 15 |
return {"location": "Paris", "temperature": "22", "unit": "celsius"}
|
| 16 |
else:
|
| 17 |
return {"location": location, "temperature": "unknown", "unit": unit}
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def generate_with_function_calling(self, messages, functions):
|
| 27 |
-
try:
|
| 28 |
-
# Format messages for the model
|
| 29 |
-
response = self.client.chat_completion(
|
| 30 |
-
messages=messages,
|
| 31 |
-
max_tokens=self.max_tokens,
|
| 32 |
-
temperature=self.temperature,
|
| 33 |
-
tools=functions,
|
| 34 |
-
tool_choice="auto"
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
return response
|
| 38 |
-
except Exception as e:
|
| 39 |
-
print(f"Error in generate: {str(e)}")
|
| 40 |
-
return {"error": str(e)}
|
| 41 |
-
|
| 42 |
-
def call_function(self, function_name, arguments):
|
| 43 |
-
if function_name == "get_current_weather":
|
| 44 |
-
location = arguments.get("location", "")
|
| 45 |
-
unit = arguments.get("unit", "fahrenheit")
|
| 46 |
-
return get_current_weather(location, unit)
|
| 47 |
-
return {"error": f"Function {function_name} not found"}
|
| 48 |
-
|
| 49 |
-
# Initialize the model
|
| 50 |
-
def init_model():
|
| 51 |
-
token = os.environ.get("HUGGINGFACE_TOKEN")
|
| 52 |
-
return HfApiModel(
|
| 53 |
-
max_tokens=2096,
|
| 54 |
-
temperature=0.5,
|
| 55 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 56 |
-
)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
"type": "object",
|
| 66 |
-
"properties": {
|
| 67 |
-
"location": {
|
| 68 |
-
"type": "string",
|
| 69 |
-
"description": "The city and state, e.g. San Francisco, CA"
|
| 70 |
-
},
|
| 71 |
-
"unit": {
|
| 72 |
-
"type": "string",
|
| 73 |
-
"enum": ["celsius", "fahrenheit"],
|
| 74 |
-
"description": "The unit of temperature to use. Infer this from the user's location."
|
| 75 |
-
}
|
| 76 |
-
},
|
| 77 |
-
"required": ["location"]
|
| 78 |
-
}
|
| 79 |
-
}
|
| 80 |
-
}
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
formatted_history.append({"role": "user", "content": human})
|
| 92 |
-
if assistant: # Check if assistant response exists
|
| 93 |
-
formatted_history.append({"role": "assistant", "content": assistant})
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
# Get response from the model
|
| 100 |
-
response = process_message.model.generate_with_function_calling(
|
| 101 |
-
messages=messages,
|
| 102 |
-
functions=[weather_function]
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
# Check if there's a function call
|
| 106 |
-
if hasattr(response, "choices") and response.choices:
|
| 107 |
-
message_content = response.choices[0].message
|
| 108 |
-
|
| 109 |
-
# Check if the model wants to call a function
|
| 110 |
-
if hasattr(message_content, "tool_calls") and message_content.tool_calls:
|
| 111 |
-
tool_call = message_content.tool_calls[0]
|
| 112 |
-
function_name = tool_call.function.name
|
| 113 |
-
function_args = json.loads(tool_call.function.arguments)
|
| 114 |
-
|
| 115 |
-
# Call the function
|
| 116 |
-
function_result = process_message.model.call_function(function_name, function_args)
|
| 117 |
-
|
| 118 |
-
# Add the function result to messages
|
| 119 |
-
messages.append({
|
| 120 |
-
"role": "assistant",
|
| 121 |
-
"content": None,
|
| 122 |
-
"tool_calls": [{
|
| 123 |
-
"id": tool_call.id,
|
| 124 |
-
"type": "function",
|
| 125 |
-
"function": {
|
| 126 |
-
"name": function_name,
|
| 127 |
-
"arguments": tool_call.function.arguments
|
| 128 |
-
}
|
| 129 |
-
}]
|
| 130 |
-
})
|
| 131 |
-
|
| 132 |
-
messages.append({
|
| 133 |
-
"role": "tool",
|
| 134 |
-
"tool_call_id": tool_call.id,
|
| 135 |
-
"content": json.dumps(function_result)
|
| 136 |
-
})
|
| 137 |
-
|
| 138 |
-
# Get final response
|
| 139 |
-
final_response = process_message.model.generate_with_function_calling(
|
| 140 |
-
messages=messages,
|
| 141 |
-
functions=[weather_function]
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
if hasattr(final_response, "choices") and final_response.choices:
|
| 145 |
-
return final_response.choices[0].message.content
|
| 146 |
-
return "Error processing function result"
|
| 147 |
-
|
| 148 |
-
# If no function call, return the content directly
|
| 149 |
-
if hasattr(message_content, "content"):
|
| 150 |
-
return message_content.content
|
| 151 |
-
|
| 152 |
-
return "I couldn't process that request properly. Please try again."
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
with gr.Blocks(title="
|
| 159 |
-
gr.Markdown("#
|
| 160 |
-
gr.Markdown("
|
| 161 |
-
gr.Markdown("### Example cities with data: Tokyo, San Francisco, Paris")
|
| 162 |
|
| 163 |
chatbot = gr.ChatInterface(
|
| 164 |
-
|
| 165 |
examples=[
|
| 166 |
"What's the weather like in Tokyo?",
|
| 167 |
-
"
|
| 168 |
-
"
|
| 169 |
-
"What should I wear in Tokyo based on the weather?"
|
|
|
|
| 170 |
],
|
| 171 |
-
title="Chat with
|
| 172 |
)
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
# Launch the
|
| 175 |
if __name__ == "__main__":
|
| 176 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import gradio as gr
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 7 |
+
from llama_index.core.agent import ReActAgent
|
| 8 |
+
from llama_index.core.tools import FunctionTool
|
| 9 |
|
| 10 |
+
# Load environment variables
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Get Hugging Face token
|
| 14 |
+
hf_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 15 |
+
if not hf_token:
|
| 16 |
+
raise ValueError("Hugging Face token not found. Configure HUGGINGFACE_TOKEN in your environment variables")
|
| 17 |
+
|
| 18 |
+
# Authenticate with Hugging Face
|
| 19 |
+
login(token=hf_token)
|
| 20 |
+
|
| 21 |
+
# Define weather function with static data
|
| 22 |
+
def get_current_weather(location: str, unit: str = "fahrenheit") -> dict:
|
| 23 |
+
"""
|
| 24 |
+
Get the current weather in a given location
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
location (str): The city name, e.g. San Francisco, Tokyo
|
| 28 |
+
unit (str): The unit of temperature, either celsius or fahrenheit
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
dict: Weather information including location, temperature and unit
|
| 32 |
+
"""
|
| 33 |
+
location = location.lower()
|
| 34 |
+
|
| 35 |
+
if "tokyo" in location:
|
| 36 |
return {"location": "Tokyo", "temperature": "10", "unit": "celsius"}
|
| 37 |
+
elif "san francisco" in location:
|
| 38 |
return {"location": "San Francisco", "temperature": "72", "unit": "fahrenheit"}
|
| 39 |
+
elif "paris" in location:
|
| 40 |
return {"location": "Paris", "temperature": "22", "unit": "celsius"}
|
| 41 |
else:
|
| 42 |
return {"location": location, "temperature": "unknown", "unit": unit}
|
| 43 |
|
| 44 |
+
# Create a tool for the agent
|
| 45 |
+
weather_tool = FunctionTool.from_defaults(
|
| 46 |
+
name="get_current_weather",
|
| 47 |
+
fn=get_current_weather,
|
| 48 |
+
description="Get the current weather in a given location"
|
| 49 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Configure the language model
|
| 52 |
+
llm = HuggingFaceInferenceAPI(
|
| 53 |
+
model_name="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 54 |
+
temperature=0.7,
|
| 55 |
+
max_tokens=512,
|
| 56 |
+
token=hf_token,
|
| 57 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Create the agent with an appropriate system prompt
|
| 60 |
+
agent = ReActAgent.from_tools(
|
| 61 |
+
[weather_tool],
|
| 62 |
+
llm=llm,
|
| 63 |
+
verbose=False,
|
| 64 |
+
system_prompt="""
|
| 65 |
+
You are a helpful weather assistant that can provide current weather information for various cities.
|
| 66 |
|
| 67 |
+
Your capabilities:
|
| 68 |
+
1. Get current weather information for cities like Tokyo, San Francisco, and Paris
|
| 69 |
+
2. Provide temperature in either celsius or fahrenheit
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
When handling user requests:
|
| 72 |
+
1. If a user asks about weather in Tokyo, San Francisco, or Paris, use the get_current_weather function
|
| 73 |
+
2. For other locations, inform the user that we only have data for Tokyo, San Francisco, and Paris
|
| 74 |
|
| 75 |
+
Always provide clear, concise responses with the location, temperature, and unit of measurement.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
Example responses:
|
| 78 |
+
- "The current weather in Tokyo is 10°C."
|
| 79 |
+
- "San Francisco's temperature is currently 72°F."
|
| 80 |
+
- "Paris is experiencing a temperature of 22°C today."
|
| 81 |
+
|
| 82 |
+
Keep responses friendly and helpful, focusing on the weather information requested.
|
| 83 |
+
"""
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def respond(message, history):
|
| 87 |
+
# Execute the agent with user input
|
| 88 |
+
response = agent.chat(message)
|
| 89 |
+
return str(response)
|
| 90 |
|
| 91 |
+
# Create Gradio interface
|
| 92 |
+
with gr.Blocks(title="Weather Assistant") as demo:
|
| 93 |
+
gr.Markdown("# 🌤️ Weather Assistant")
|
| 94 |
+
gr.Markdown("### Ask about the weather in Tokyo, San Francisco, or Paris")
|
|
|
|
| 95 |
|
| 96 |
chatbot = gr.ChatInterface(
|
| 97 |
+
respond,
|
| 98 |
examples=[
|
| 99 |
"What's the weather like in Tokyo?",
|
| 100 |
+
"How's the weather in San Francisco?",
|
| 101 |
+
"Tell me about the current weather in Paris",
|
| 102 |
+
"What should I wear in Tokyo based on the weather?",
|
| 103 |
+
"Is it warm in San Francisco?"
|
| 104 |
],
|
| 105 |
+
title="Chat with Weather Assistant"
|
| 106 |
)
|
| 107 |
+
|
| 108 |
+
gr.Markdown("### Built with LlamaIndex and Qwen2.5-Coder-32B-Instruct")
|
| 109 |
|
| 110 |
+
# Launch the application
|
| 111 |
if __name__ == "__main__":
|
| 112 |
demo.launch()
|