MathieuTrachino's picture
Update app.py
37d956a verified
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')))