Fix 8
Browse files
app.py
CHANGED
|
@@ -1,26 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
|
|
|
| 13 |
|
| 14 |
def generate_response(message, temperature=0.7):
|
| 15 |
try:
|
| 16 |
-
response =
|
| 17 |
-
|
| 18 |
-
messages=[{"role": "user", "content": message}],
|
| 19 |
temperature=temperature,
|
| 20 |
-
|
| 21 |
-
|
| 22 |
)
|
| 23 |
-
return response
|
| 24 |
except Exception as e:
|
| 25 |
return f"Une erreur s'est produite : {str(e)}"
|
| 26 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
import torch
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
from huggingface_hub import login
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
+
# Login to Hugging Face
|
| 11 |
+
hf_token = os.getenv('HF_TOKEN')
|
| 12 |
+
login(hf_token)
|
| 13 |
+
|
| 14 |
+
# Configuration du modèle
|
| 15 |
+
model_path = "Nac31/Sacha-Mistral-0"
|
| 16 |
+
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16
|
| 17 |
+
|
| 18 |
+
# Initialisation du modèle
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
model_path,
|
| 22 |
+
device_map="auto",
|
| 23 |
+
torch_dtype=dtype
|
| 24 |
)
|
| 25 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 26 |
|
| 27 |
def generate_response(message, temperature=0.7):
|
| 28 |
try:
|
| 29 |
+
response = pipe(
|
| 30 |
+
message,
|
|
|
|
| 31 |
temperature=temperature,
|
| 32 |
+
max_new_tokens=500,
|
| 33 |
+
do_sample=True
|
| 34 |
)
|
| 35 |
+
return response[0]['generated_text']
|
| 36 |
except Exception as e:
|
| 37 |
return f"Une erreur s'est produite : {str(e)}"
|
| 38 |
|