Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
-
import random
|
| 5 |
|
| 6 |
-
# Configuration de l'API
|
| 7 |
API_URL = "https://api-inference.huggingface.co/models/gpt2"
|
| 8 |
api_token = os.environ.get("TOKEN")
|
| 9 |
headers = {"Authorization": f"Bearer {api_token}"}
|
|
@@ -13,38 +11,42 @@ def query(payload):
|
|
| 13 |
return response.json()
|
| 14 |
|
| 15 |
def generate_yes_no_response(question):
|
| 16 |
-
# Créez un prompt qui
|
| 17 |
-
prompt = f"
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
if isinstance(response, list) and len(response) > 0:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
return "Oui"
|
| 31 |
-
elif "non" in full_response.lower():
|
| 32 |
-
return "Non"
|
| 33 |
-
else:
|
| 34 |
-
# Si la réponse n'est ni oui ni non, choisissez aléatoirement
|
| 35 |
-
return random.choice(["Oui", "Non"])
|
| 36 |
|
| 37 |
-
# Fonction pour l'interface Gradio
|
| 38 |
def chatbot(message, history):
|
| 39 |
response = generate_yes_no_response(message)
|
| 40 |
return response
|
| 41 |
|
| 42 |
-
# Création de l'interface Gradio
|
| 43 |
iface = gr.ChatInterface(
|
| 44 |
fn=chatbot,
|
| 45 |
title="Chatbot Oui/Non",
|
| 46 |
description="Posez une question, et je répondrai par Oui ou Non."
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# Lancement de l'interface
|
| 50 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
| 4 |
|
|
|
|
| 5 |
API_URL = "https://api-inference.huggingface.co/models/gpt2"
|
| 6 |
api_token = os.environ.get("TOKEN")
|
| 7 |
headers = {"Authorization": f"Bearer {api_token}"}
|
|
|
|
| 11 |
return response.json()
|
| 12 |
|
| 13 |
def generate_yes_no_response(question):
|
| 14 |
+
# Créez un prompt qui force le modèle à choisir entre Oui et Non
|
| 15 |
+
prompt = f"Question : {question}\nRéponse (Oui ou Non) :"
|
| 16 |
|
| 17 |
+
# Configurez les paramètres pour forcer une réponse courte
|
| 18 |
+
payload = {
|
| 19 |
+
"inputs": prompt,
|
| 20 |
+
"parameters": {
|
| 21 |
+
"max_new_tokens": 1,
|
| 22 |
+
"return_full_text": False,
|
| 23 |
+
"do_sample": True,
|
| 24 |
+
"top_k": 2,
|
| 25 |
+
"temperature": 0.7
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
response = query(payload)
|
| 30 |
+
|
| 31 |
+
# Assurez-vous que la réponse est soit "Oui" soit "Non"
|
| 32 |
if isinstance(response, list) and len(response) > 0:
|
| 33 |
+
answer = response[0].get('generated_text', '').strip().lower()
|
| 34 |
+
if answer == "oui":
|
| 35 |
+
return "Oui"
|
| 36 |
+
elif answer == "non":
|
| 37 |
+
return "Non"
|
| 38 |
|
| 39 |
+
# Si la réponse n'est pas "Oui" ou "Non", retournez une réponse par défaut
|
| 40 |
+
return "Réponse invalide"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
def chatbot(message, history):
|
| 43 |
response = generate_yes_no_response(message)
|
| 44 |
return response
|
| 45 |
|
|
|
|
| 46 |
iface = gr.ChatInterface(
|
| 47 |
fn=chatbot,
|
| 48 |
title="Chatbot Oui/Non",
|
| 49 |
description="Posez une question, et je répondrai par Oui ou Non."
|
| 50 |
)
|
| 51 |
|
|
|
|
| 52 |
iface.launch()
|