|
import requests |
|
import os |
|
import json |
|
import gradio as gr |
|
|
|
|
|
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' |
|
} |
|
|
|
|
|
with open("tools-intent-detection.json", "r", encoding="utf-8") as file: |
|
tools = json.load(file) |
|
|
|
|
|
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 = [ |
|
{ |
|
"role": "system", |
|
"content": "" |
|
}, |
|
{ |
|
"role": "user", |
|
"content": user_prompt |
|
} |
|
] |
|
|
|
|
|
data = { |
|
"model": "gpt-4o", |
|
"messages": messages, |
|
"tools": tools, |
|
"tool_choice": "required" |
|
} |
|
|
|
|
|
response = requests.post(url, headers=headers, data=json.dumps(data)) |
|
|
|
|
|
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"] |
|
|
|
|
|
if 'tool_calls' in reply and len(reply['tool_calls']) > 0: |
|
function_name = reply['tool_calls'][0]['function']['name'] |
|
|
|
category = function_mapping.get(function_name, "0") |
|
return function_name, category |
|
else: |
|
return "No function called", "0" |
|
else: |
|
return "Unexpected response format.", "0" |
|
|
|
|
|
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'))) |