MathieuTrachino commited on
Commit
95b36d4
·
verified ·
1 Parent(s): 459ed38

Update app.py

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