Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,102 +0,0 @@
|
|
1 |
-
import json
|
2 |
-
import os
|
3 |
-
import gradio as gr
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
-
|
6 |
-
# Function to call the Llama 3.1 8B model through Hugging Face API
|
7 |
-
def call_llama_model(user_query):
|
8 |
-
# Initialize the inference client with access token
|
9 |
-
client = InferenceClient(
|
10 |
-
"meta-llama/Meta-Llama-3.1-8B-Instruct",
|
11 |
-
token=os.environ.get("HF_TOKEN")
|
12 |
-
)
|
13 |
-
|
14 |
-
# Define the addition function schema
|
15 |
-
function_schema = {
|
16 |
-
"name": "add_numbers",
|
17 |
-
"description": "Add two numbers together",
|
18 |
-
"parameters": {
|
19 |
-
"type": "object",
|
20 |
-
"properties": {
|
21 |
-
"num1": {
|
22 |
-
"type": "number",
|
23 |
-
"description": "First number to add"
|
24 |
-
},
|
25 |
-
"num2": {
|
26 |
-
"type": "number",
|
27 |
-
"description": "Second number to add"
|
28 |
-
}
|
29 |
-
},
|
30 |
-
"required": ["num1", "num2"]
|
31 |
-
}
|
32 |
-
}
|
33 |
-
|
34 |
-
# Create the system prompt with function definition
|
35 |
-
system_prompt = f"""You have access to the following function:
|
36 |
-
{json.dumps(function_schema, indent=2)}
|
37 |
-
|
38 |
-
When given a query about adding numbers, you must:
|
39 |
-
1. Extract the two numbers from the query
|
40 |
-
2. Call the add_numbers function with these numbers
|
41 |
-
3. Respond ONLY with a valid JSON object in this exact format:
|
42 |
-
{{"function_call": {{"name": "add_numbers", "parameters": {{"num1": [first number], "num2": [second number]}}}}}}
|
43 |
-
|
44 |
-
DO NOT include any explanation, just the JSON.
|
45 |
-
"""
|
46 |
-
|
47 |
-
# Call the model with the appropriate format for Llama 3.1
|
48 |
-
response = client.text_generation(
|
49 |
-
prompt=f"<|system|>\n{system_prompt}\n<|user|>\n{user_query}\n<|assistant|>",
|
50 |
-
max_new_tokens=256,
|
51 |
-
temperature=0.1,
|
52 |
-
return_full_text=False
|
53 |
-
)
|
54 |
-
|
55 |
-
return response
|
56 |
-
|
57 |
-
# Function to parse the model response and calculate the result
|
58 |
-
def process_addition(query):
|
59 |
-
try:
|
60 |
-
# Get model response
|
61 |
-
model_response = call_llama_model(query)
|
62 |
-
|
63 |
-
# Create a debug output with the raw response
|
64 |
-
debug_info = f"Raw model response:\n{model_response}\n\n"
|
65 |
-
|
66 |
-
# Try to parse the JSON response
|
67 |
-
try:
|
68 |
-
# Find the JSON part in the response (it might have additional text)
|
69 |
-
json_start = model_response.find('{')
|
70 |
-
json_end = model_response.rfind('}') + 1
|
71 |
-
|
72 |
-
if json_start >= 0 and json_end > json_start:
|
73 |
-
json_str = model_response[json_start:json_end]
|
74 |
-
response_data = json.loads(json_str)
|
75 |
-
debug_info += f"Parsed JSON:\n{json.dumps(response_data, indent=2)}\n\n"
|
76 |
-
else:
|
77 |
-
return f"Error: No valid JSON found in response.\n\n{debug_info}"
|
78 |
-
|
79 |
-
# Check if it has a function call
|
80 |
-
if "function_call" in response_data:
|
81 |
-
function_name = response_data["function_call"]["name"]
|
82 |
-
params = response_data["function_call"]["parameters"]
|
83 |
-
|
84 |
-
if function_name == "add_numbers":
|
85 |
-
# Extract the numbers
|
86 |
-
num1 = float(params["num1"])
|
87 |
-
num2 = float(params["num2"])
|
88 |
-
result = num1 + num2
|
89 |
-
|
90 |
-
# Return a formatted response
|
91 |
-
return f"""
|
92 |
-
### Input Processed Successfully
|
93 |
-
|
94 |
-
**Numbers extracted:** {num1} and {num2}
|
95 |
-
|
96 |
-
**Function called:** `{function_name}`
|
97 |
-
|
98 |
-
**Result:** {result}
|
99 |
-
|
100 |
-
**JSON Function Call:**
|
101 |
-
```json
|
102 |
-
{json.dumps(response_data, indent=2)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|