Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from unsloth import FastLanguageModel
|
| 4 |
+
from transformers import TextStreamer
|
| 5 |
+
|
| 6 |
+
# Configuration du modèle
|
| 7 |
+
max_seq_length = 2048
|
| 8 |
+
dtype = None
|
| 9 |
+
load_in_4bit = True
|
| 10 |
+
|
| 11 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 12 |
+
model_name="Thomslionel/mistral_for_story_generation",
|
| 13 |
+
max_seq_length=max_seq_length,
|
| 14 |
+
dtype=dtype,
|
| 15 |
+
load_in_4bit=load_in_4bit,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
model = FastLanguageModel.for_inference(model)
|
| 19 |
+
|
| 20 |
+
EOS_TOKEN = tokenizer.eos_token
|
| 21 |
+
INSTRUCTION_KEY = "### Instruction:"
|
| 22 |
+
RESPONSE_KEY = "### Réponse:"
|
| 23 |
+
END_KEY = EOS_TOKEN
|
| 24 |
+
INTRO_BLURB = "Ci-dessous se trouve une instruction qui décrit une tâche. Écrivez une réponse qui complète adéquatement la demande."
|
| 25 |
+
PROMPT_FOR_GENERATION_FORMAT = """{intro}
|
| 26 |
+
{instruction_key}
|
| 27 |
+
{instruction}
|
| 28 |
+
{response_key}
|
| 29 |
+
{end_key}
|
| 30 |
+
""".format(
|
| 31 |
+
intro=INTRO_BLURB,
|
| 32 |
+
instruction_key=INSTRUCTION_KEY,
|
| 33 |
+
instruction="{instruction}",
|
| 34 |
+
response_key=RESPONSE_KEY,
|
| 35 |
+
end_key=END_KEY,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Fonction pour créer le prompt avec le titre
|
| 39 |
+
def create_prompt_with_title(titre):
|
| 40 |
+
instruction = (
|
| 41 |
+
f"Tu es un écrivain talentueux spécialisé dans les histoires culturelles Burkinabè comiques et narratives. "
|
| 42 |
+
f"Ton objectif est de créer une histoire captivante, drôle, culturelle et cohérente. "
|
| 43 |
+
f"Le titre de l'histoire est : {titre}. "
|
| 44 |
+
f"Assure-toi d'inclure des éléments humoristiques, des personnages intéressants et une intrigue bien développée. "
|
| 45 |
+
f"Voici le titre : {titre}. Bonne écriture !"
|
| 46 |
+
)
|
| 47 |
+
return f"### Instruction:\n{instruction}\n### Response:\n"
|
| 48 |
+
|
| 49 |
+
# Interface Streamlit
|
| 50 |
+
st.title("Générateur d'Histoires Burkinabè")
|
| 51 |
+
titre = st.text_input("Entrez le titre de l'histoire", "M'BA SOAMBA (le lièvre) et M'BA BAAGA (le chien)")
|
| 52 |
+
|
| 53 |
+
if st.button("Générer l'histoire"):
|
| 54 |
+
prompt = create_prompt_with_title(titre)
|
| 55 |
+
inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
|
| 56 |
+
text_streamer = TextStreamer(tokenizer)
|
| 57 |
+
|
| 58 |
+
with st.spinner("Génération de l'histoire..."):
|
| 59 |
+
generated_text = model.generate(
|
| 60 |
+
**inputs,
|
| 61 |
+
streamer=text_streamer,
|
| 62 |
+
max_new_tokens=2048,
|
| 63 |
+
temperature=1.9,
|
| 64 |
+
top_p=0.9,
|
| 65 |
+
top_k=50,
|
| 66 |
+
repetition_penalty=1.3
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
st.success("Histoire générée avec succès!")
|
| 70 |
+
st.text_area("Histoire générée", value=generated_text, height=400)
|