Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 6 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
| 7 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 8 |
+
|
| 9 |
+
def generate_suggestion(mots):
|
| 10 |
+
prompt = f"""
|
| 11 |
+
Tu es un thérapeute-poète.
|
| 12 |
+
Voici des mots sensoriels ou symboliques : {mots}.
|
| 13 |
+
Rédige 5 phrases poétiques, variées, sensibles.
|
| 14 |
+
Aucune structure figée. Évoque des sensations, des images, des émotions.
|
| 15 |
+
Termine par •°○
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
+
response = requests.post(API_URL, headers=headers, json={
|
| 18 |
+
"inputs": prompt,
|
| 19 |
+
"parameters": {
|
| 20 |
+
"temperature": 0.95,
|
| 21 |
+
"top_p": 0.9,
|
| 22 |
+
"max_new_tokens": 180,
|
| 23 |
+
"do_sample": True
|
| 24 |
+
}
|
| 25 |
+
})
|
| 26 |
+
|
| 27 |
+
output = response.json()
|
| 28 |
+
if isinstance(output, list) and "generated_text" in output[0]:
|
| 29 |
+
return output[0]["generated_text"]
|
| 30 |
+
elif "generated_text" in output:
|
| 31 |
+
return output["generated_text"]
|
| 32 |
+
else:
|
| 33 |
+
return "⚠️ Erreur de génération IA."
|
| 34 |
+
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=generate_suggestion,
|
| 37 |
+
inputs=gr.Textbox(label="Mots ou question", placeholder="frisson, vertige, lumière..."),
|
| 38 |
+
outputs="text",
|
| 39 |
+
title="Résonance Hypnopoétique •°○",
|
| 40 |
+
description="Une suggestion poétique inspirée de vos mots-clés sensoriels ou symboliques."
|
| 41 |
)
|
| 42 |
|
| 43 |
+
iface.launch()
|
|
|
|
|
|