Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,33 @@
|
|
| 1 |
-
from transformers import
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
|
| 5 |
-
pipe = pipeline("text-generation", model="eduard76/Llama3-8b-good", trust_remote_code=True)
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import torch
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
model_id = "eduard76/Llama3-8b-good-new"
|
|
|
|
| 6 |
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
device_map="auto", # automatically uses GPU if available
|
| 11 |
+
torch_dtype=torch.float16,
|
| 12 |
+
load_in_4bit=True,
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
+
model.eval()
|
| 16 |
|
| 17 |
+
def chat(user_input, history):
|
| 18 |
+
history_text = "\n".join([f"User: {u}\nAI: {a}" for u, a in history])
|
| 19 |
+
prompt = f"{history_text}\nUser: {user_input}\nAI:"
|
| 20 |
+
|
| 21 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model.generate(
|
| 24 |
+
**inputs,
|
| 25 |
+
max_new_tokens=512,
|
| 26 |
+
do_sample=True,
|
| 27 |
+
temperature=0.001
|
| 28 |
+
)
|
| 29 |
+
generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
+
answer = generated.split("AI:")[-1].strip()
|
| 31 |
+
return answer
|
| 32 |
+
|
| 33 |
+
gr.ChatInterface(chat, title="💬 Chat with first Eduard LLM").launch()
|