Upload app.py
Browse files
app.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()
|