MathieuTrachino commited on
Commit
eb77234
·
verified ·
1 Parent(s): b519f7b

Upload 2 files

Browse files
Files changed (2) hide show
  1. gradio_iface.py +91 -0
  2. tools-intent-detection.json +82 -0
gradio_iface.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
tools-intent-detection.json ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "type": "function",
4
+ "function": {
5
+ "name": "Creation_dossier_kbis",
6
+ "description": "L'utilisateur partage un extrait de kbis ou souhaite créer un dossier",
7
+ "parameters": { "type": "object", "properties": {} }
8
+ }
9
+ },
10
+ {
11
+ "type": "function",
12
+ "function": {
13
+ "name": "Redaction_non_juridique",
14
+ "description": "Redaction de correspondances exclusivement non juridiques (email, courrier, lettre, newsletter ou publication réseaux sociaux)",
15
+ "parameters": { "type": "object", "properties": {} }
16
+ }
17
+ },
18
+ {
19
+ "type": "function",
20
+ "function": {
21
+ "name": "Redaction_juridique",
22
+ "description": "Redaction de contenu juridique",
23
+ "parameters": { "type": "object", "properties": {} }
24
+ }
25
+ },
26
+ {
27
+ "type": "function",
28
+ "function": {
29
+ "name": "Resume",
30
+ "description": "Resume un texte",
31
+ "parameters": { "type": "object", "properties": {} }
32
+ }
33
+ },
34
+ {
35
+ "type": "function",
36
+ "function": {
37
+ "name": "Salutations",
38
+ "description": "Reponse aux salutations et demande les fonctionnalités de l'assistant IA",
39
+ "parameters": { "type": "object", "properties": {} }
40
+ }
41
+ },
42
+ {
43
+ "type": "function",
44
+ "function": {
45
+ "name": "Traduction",
46
+ "description": "Traduction de contenu en une autre langue",
47
+ "parameters": { "type": "object", "properties": {} }
48
+ }
49
+ },
50
+ {
51
+ "type": "function",
52
+ "function": {
53
+ "name": "Information_utilisateur",
54
+ "description": "Informations sur les utilisateurs (id, type, etc.)",
55
+ "parameters": { "type": "object", "properties": {} }
56
+ }
57
+ },
58
+ {
59
+ "type": "function",
60
+ "function": {
61
+ "name": "Information_dossier",
62
+ "description": "Informations generales et financieres sur les dossiers",
63
+ "parameters": { "type": "object", "properties": {} }
64
+ }
65
+ },
66
+ {
67
+ "type": "function",
68
+ "function": {
69
+ "name": "Information_personne_societe",
70
+ "description": "Informations et factures d'une personne ou une société",
71
+ "parameters": { "type": "object", "properties": {} }
72
+ }
73
+ },
74
+ {
75
+ "type": "function",
76
+ "function": {
77
+ "name": "Autre_demande",
78
+ "description": "Traite toutes les demandes qui ne sont pas couvertes par les autres fonctions",
79
+ "parameters": { "type": "object", "properties": {} }
80
+ }
81
+ }
82
+ ]