Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,85 @@
|
|
1 |
-
import requests
|
2 |
-
import os
|
3 |
-
import json
|
4 |
-
import gradio as gr
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
"
|
15 |
-
"
|
16 |
-
"
|
17 |
-
"
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
outputs=[
|
86 |
-
gr.Textbox(label="Intention"),
|
87 |
-
gr.Textbox(label="Category")
|
88 |
-
]
|
89 |
-
)
|
90 |
-
|
91 |
iface.launch()
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Mapping function to class
|
7 |
+
function_mapping = {
|
8 |
+
"Creation_dossier_kbis": 'POST',
|
9 |
+
"Redaction_non_juridique": 'DRAFT',
|
10 |
+
"Redaction_juridique": '0',
|
11 |
+
"Resume": '0',
|
12 |
+
"Salutations": 'DRAFT',
|
13 |
+
"Traduction": 'DRAFT',
|
14 |
+
"Information_utilisateur": 'GET',
|
15 |
+
"Information_dossier": 'GET',
|
16 |
+
"Information_personne_societe": 'GET',
|
17 |
+
"Autre_demande": '0'
|
18 |
+
}
|
19 |
+
|
20 |
+
# Load tools
|
21 |
+
with open("tools-intent-detection.json", "r", encoding="utf-8") as file:
|
22 |
+
tools = json.load(file)
|
23 |
+
|
24 |
+
# API configuration
|
25 |
+
url = 'https://openai-dev-fra-001.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-10-21'
|
26 |
+
headers = {
|
27 |
+
'api-key': API_KEY,
|
28 |
+
'Content-Type': 'application/json'
|
29 |
+
}
|
30 |
+
|
31 |
+
def get_model_response(user_prompt):
|
32 |
+
# messages list
|
33 |
+
messages = [
|
34 |
+
{
|
35 |
+
"role": "system",
|
36 |
+
"content": ""
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"role": "user",
|
40 |
+
"content": user_prompt
|
41 |
+
}
|
42 |
+
]
|
43 |
+
|
44 |
+
# payload
|
45 |
+
data = {
|
46 |
+
"model": "gpt-4o",
|
47 |
+
"messages": messages,
|
48 |
+
"tools": tools,
|
49 |
+
"tool_choice": "required"
|
50 |
+
}
|
51 |
+
|
52 |
+
# API call
|
53 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
54 |
+
|
55 |
+
# Process the response
|
56 |
+
if response.status_code != 200:
|
57 |
+
return f"Error: {response.status_code} - {response.text}", "0"
|
58 |
+
else:
|
59 |
+
response_data = response.json()
|
60 |
+
if 'choices' in response_data:
|
61 |
+
reply = response_data["choices"][0]["message"]
|
62 |
+
|
63 |
+
# Extract function name from tool_calls
|
64 |
+
if 'tool_calls' in reply and len(reply['tool_calls']) > 0:
|
65 |
+
function_name = reply['tool_calls'][0]['function']['name']
|
66 |
+
# Get the corresponding class from the mapping
|
67 |
+
category = function_mapping.get(function_name, "0") # 0 is default if function name not found
|
68 |
+
return function_name, category
|
69 |
+
else:
|
70 |
+
return "No function called", "0"
|
71 |
+
else:
|
72 |
+
return "Unexpected response format.", "0"
|
73 |
+
|
74 |
+
# Gradio app
|
75 |
+
iface = gr.Interface(
|
76 |
+
title="Intent Detection Playground",
|
77 |
+
fn=get_model_response,
|
78 |
+
inputs=gr.Textbox(label="User Prompt"),
|
79 |
+
outputs=[
|
80 |
+
gr.Textbox(label="Intention"),
|
81 |
+
gr.Textbox(label="Category")
|
82 |
+
]
|
83 |
+
)
|
84 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
iface.launch()
|