Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
chatbot = gr.ChatInterface(
|
| 24 |
respond,
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 28 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 29 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 30 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 31 |
-
],
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
with demo:
|
| 36 |
-
chatbot.render()
|
| 37 |
-
|
| 38 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
MODEL = "meta-llama/Llama-3.2-3B-Instruct"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
MODEL,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def respond(message, history):
|
| 15 |
+
history_text = ""
|
| 16 |
+
for user, bot in history:
|
| 17 |
+
history_text += f"User: {user}\nAssistant: {bot}\n"
|
| 18 |
+
prompt = history_text + f"User: {message}\nAssistant:"
|
| 19 |
+
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 21 |
+
|
| 22 |
+
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
output = model.generate(
|
| 25 |
+
**inputs,
|
| 26 |
+
max_new_tokens=300,
|
| 27 |
+
temperature=0.7,
|
| 28 |
+
top_p=0.9,
|
| 29 |
+
streamer=streamer
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 33 |
+
return output_text.split("Assistant:")[-1].strip()
|
| 34 |
|
| 35 |
chatbot = gr.ChatInterface(
|
| 36 |
respond,
|
| 37 |
+
title="Free Llama 3.2 Chatbot",
|
| 38 |
+
description="Runs completely free — no credits, no HF token needed."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
+
chatbot.launch()
|
|
|
|
|
|
|
|
|
|
|
|