Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,44 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import spaces
|
3 |
-
import transformers
|
4 |
-
from transformers import AutoTokenizer,AutoModelForCausalLM
|
5 |
-
from transformers import pipeline
|
6 |
-
import torch
|
7 |
-
import os
|
8 |
|
9 |
-
api_token = os.environ.get("APIKEY")
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
@spaces.GPU
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
model=model,
|
30 |
-
tokenizer=model.config.tokenizer,
|
31 |
-
device_map="auto",
|
32 |
-
)
|
33 |
-
|
34 |
-
messages = [
|
35 |
-
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
|
36 |
-
{"role": "user", "content": "Who are you?"},
|
37 |
-
]
|
38 |
-
|
39 |
-
terminators = [
|
40 |
-
pipeline.tokenizer.eos_token_id,
|
41 |
-
pipeline.tokenizer.convert_tokens_to_ids("")
|
42 |
-
]
|
43 |
-
|
44 |
-
# Utiliser le pipeline pour générer du texte
|
45 |
-
outputs = pipeline(
|
46 |
-
messages,
|
47 |
-
max_new_tokens=256,
|
48 |
-
eos_token_id=terminators,
|
49 |
-
do_sample=True,
|
50 |
-
temperature=0.6,
|
51 |
-
top_p=0.9,
|
52 |
-
)
|
53 |
-
|
54 |
-
|
55 |
-
# Fonction de génération de texte
|
56 |
-
def generate_text(prompt):
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
response_ids = model.generate(inputs.input_ids)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
def chatbot(message, history):
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
import spaces
|
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
6 |
|
7 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
|
8 |
+
api_token = os.environ.get("TOKEN")
|
9 |
+
headers = {"Authorization": f"Bearer {api_token}"}
|
10 |
+
@spaces.GPU
|
11 |
+
def query(payload):
|
12 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
13 |
+
return response.json()
|
14 |
+
|
15 |
+
def generate_response(prompt):
|
16 |
+
payload = {
|
17 |
+
"inputs": prompt,
|
18 |
+
"parameters": {
|
19 |
+
"max_new_tokens": 100,
|
20 |
+
"temperature": 0.7,
|
21 |
+
"top_p": 0.95,
|
22 |
+
"do_sample": True
|
23 |
+
}
|
24 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
response = query(payload)
|
|
|
|
|
27 |
|
28 |
+
if isinstance(response, list) and len(response) > 0:
|
29 |
+
return response[0].get('generated_text', '')
|
30 |
+
elif isinstance(response, dict) and 'generated_text' in response:
|
31 |
+
return response['generated_text']
|
32 |
+
return "Désolé, je n'ai pas pu générer de réponse."
|
33 |
|
34 |
def chatbot(message, history):
|
35 |
+
response = generate_response(message)
|
36 |
+
return response
|
37 |
+
|
38 |
+
iface = gr.ChatInterface(
|
39 |
+
fn=chatbot,
|
40 |
+
title="Chatbot Meta-Llama-3-8B-Instruct",
|
41 |
+
description="Interagissez avec le modèle Meta-Llama-3-8B-Instruct."
|
42 |
+
)
|
43 |
+
|
44 |
+
iface.launch()
|