File size: 2,443 Bytes
95b36d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f637e61
95b36d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37d956a
1
2
3
4
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
import requests
import os
import json
import gradio as gr

# Mapping function to class
function_mapping = {
    "Creation_dossier_kbis": 'POST',
    "Redaction_non_juridique": 'DRAFT',
    "Redaction_juridique": '0',
    "Resume": '0',
    "Salutations": 'DRAFT',
    "Traduction": 'DRAFT',
    "Information_utilisateur": 'GET',
    "Information_dossier": 'GET',
    "Information_personne_societe": 'GET',
    "Autre_demande": '0'
}

# Load tools
with open("tools-intent-detection.json", "r", encoding="utf-8") as file:
    tools = json.load(file)

# API configuration
url = 'https://openai-dev-fra-001.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-10-21'
headers = {
    'api-key': os.getenv('API_KEY'),
    'Content-Type': 'application/json'
}

def get_model_response(user_prompt):
    # messages list
    messages = [
        {
            "role": "system",
            "content": ""
        },
        {
            "role": "user",
            "content": user_prompt
        }
    ]
    
    # payload
    data = {
        "model": "gpt-4o",
        "messages": messages,
        "tools": tools,
        "tool_choice": "required"
    }
    
    # API call
    response = requests.post(url, headers=headers, data=json.dumps(data))
    
    # Process the response
    if response.status_code != 200:
        return f"Error: {response.status_code} - {response.text}", "0"
    else:
        response_data = response.json()
        if 'choices' in response_data:
            reply = response_data["choices"][0]["message"]
            
            # Extract function name from tool_calls
            if 'tool_calls' in reply and len(reply['tool_calls']) > 0:
                function_name = reply['tool_calls'][0]['function']['name']
                # Get the corresponding class from the mapping
                category = function_mapping.get(function_name, "0")  # 0 is default if function name not found
                return function_name, category
            else:
                return "No function called", "0"
        else:
            return "Unexpected response format.", "0"

# Gradio app
iface = gr.Interface(
    title="Intent Detection Playground",
    fn=get_model_response,
    inputs=gr.Textbox(label="User Prompt"),
    outputs=[
        gr.Textbox(label="Intention"),
        gr.Textbox(label="Category")
    ]
)

iface.launch(share=True, auth=(os.getenv('USERNAME'), os.getenv('PASSWORD')))