Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
919bf29
1
Parent(s):
f38f71f
Rewrite with transformers
Browse files
app.py
CHANGED
|
@@ -1,7 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def respond(
|
| 7 |
message,
|
|
@@ -10,45 +18,32 @@ def respond(
|
|
| 10 |
max_tokens,
|
| 11 |
temperature,
|
| 12 |
top_p,
|
| 13 |
-
hf_token: gr.OAuthToken,
|
| 14 |
):
|
| 15 |
-
"""
|
| 16 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 17 |
-
"""
|
| 18 |
-
client = InferenceClient(token=hf_token.token, model="le-llm/gemma-3-12b-it-reasoning")
|
| 19 |
-
|
| 20 |
-
messages = [{"role": "system", "content": system_message}]
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
messages,
|
| 30 |
-
max_tokens=max_tokens,
|
| 31 |
-
stream=True,
|
| 32 |
temperature=temperature,
|
| 33 |
top_p=top_p,
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
token = ""
|
| 37 |
-
if len(choices) and choices[0].delta.content:
|
| 38 |
-
token = choices[0].delta.content
|
| 39 |
-
|
| 40 |
-
response += token
|
| 41 |
-
yield response
|
| 42 |
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
"""
|
| 45 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 46 |
-
"""
|
| 47 |
chatbot = gr.ChatInterface(
|
| 48 |
respond,
|
| 49 |
type="messages",
|
| 50 |
additional_inputs=[
|
| 51 |
-
gr.Textbox(value=
|
| 52 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 53 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 54 |
gr.Slider(
|
|
@@ -64,6 +59,5 @@ chatbot = gr.ChatInterface(
|
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
chatbot.render()
|
| 66 |
|
| 67 |
-
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
+
MODEL_ID = "le-llm/gemma-3-12b-it-reasoning"
|
| 6 |
+
|
| 7 |
+
# Load model & tokenizer
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float16 if device=="cuda" else torch.float32).to(device)
|
| 11 |
+
|
| 12 |
+
SYSTEM_PROMPT = "You are a friendly Chatbot."
|
| 13 |
|
| 14 |
def respond(
|
| 15 |
message,
|
|
|
|
| 18 |
max_tokens,
|
| 19 |
temperature,
|
| 20 |
top_p,
|
|
|
|
| 21 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
conversation = system_message + "\n"
|
| 24 |
+
for turn in history:
|
| 25 |
+
role = "User" if turn["role"] == "user" else "Assistant"
|
| 26 |
+
conversation += f"{role}: {turn['content']}\n"
|
| 27 |
+
conversation += f"User: {message}\nAssistant:"
|
| 28 |
|
| 29 |
+
inputs = tokenizer(conversation, return_tensors="pt").to(device)
|
| 30 |
|
| 31 |
+
output_ids = model.generate(
|
| 32 |
+
**inputs,
|
| 33 |
+
max_new_tokens=max_tokens,
|
|
|
|
|
|
|
|
|
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
response = tokenizer.decode(output_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 40 |
+
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
chatbot = gr.ChatInterface(
|
| 43 |
respond,
|
| 44 |
type="messages",
|
| 45 |
additional_inputs=[
|
| 46 |
+
gr.Textbox(value=SYSTEM_PROMPT, label="System message"),
|
| 47 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 48 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 49 |
gr.Slider(
|
|
|
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
chatbot.render()
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
demo.launch()
|