Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
"""
|
| 5 |
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
|
|
@@ -7,62 +18,65 @@ For more information on `huggingface_hub` Inference API support, please check th
|
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
-
additional_inputs_accordion=gr.Accordion(
|
| 49 |
-
label="⚙️ Parameters", open=False, render=False
|
| 50 |
-
),
|
| 51 |
additional_inputs=[
|
| 52 |
-
gr.Textbox(value="You are a
|
| 53 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 54 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 55 |
-
gr.Slider(
|
| 56 |
-
minimum=0.1,
|
| 57 |
-
maximum=1.0,
|
| 58 |
-
value=0.95,
|
| 59 |
-
step=0.05,
|
| 60 |
-
label="Top-p (nucleus sampling)",
|
| 61 |
-
),
|
| 62 |
],
|
| 63 |
theme="Ocean",
|
| 64 |
)
|
| 65 |
|
| 66 |
-
|
| 67 |
if __name__ == "__main__":
|
| 68 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, Gemma3ForCausalLM
|
| 5 |
+
|
| 6 |
+
model_path = "SRP-base-model-training/gemma_3_800M_sft_v2_translation-kazparc_latest"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 8 |
+
model = Gemma3ForCausalLM.from_pretrained(
|
| 9 |
+
model_path,
|
| 10 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
model.eval()
|
| 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
|
|
|
|
| 18 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 19 |
|
| 20 |
|
| 21 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
messages = [{"role": "system", "content": system_message}]
|
| 23 |
+
|
| 24 |
+
# Rebuild full chat history
|
| 25 |
+
for user_msg, assistant_msg in history:
|
| 26 |
+
if user_msg:
|
| 27 |
+
messages.append({"role": "user", "content": user_msg})
|
| 28 |
+
if assistant_msg:
|
| 29 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 30 |
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
+
# Convert chat to single prompt
|
| 33 |
+
prompt = ""
|
| 34 |
+
for msg in messages:
|
| 35 |
+
role = msg["role"]
|
| 36 |
+
content = msg["content"]
|
| 37 |
+
if role == "system":
|
| 38 |
+
prompt += f"[SYSTEM] {content}\n"
|
| 39 |
+
elif role == "user":
|
| 40 |
+
prompt += f"[USER] {content}\n"
|
| 41 |
+
elif role == "assistant":
|
| 42 |
+
prompt += f"[ASSISTANT] {content}\n"
|
| 43 |
+
prompt += "[ASSISTANT]"
|
| 44 |
|
| 45 |
+
# Tokenize
|
| 46 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 47 |
+
input_len = inputs["input_ids"].shape[-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Generate tokens (with streaming behavior)
|
| 50 |
+
generated_text = ""
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
output_ids = model.generate(
|
| 53 |
+
**inputs,
|
| 54 |
+
max_new_tokens=max_tokens,
|
| 55 |
+
do_sample=True,
|
| 56 |
+
temperature=temperature,
|
| 57 |
+
top_p=top_p,
|
| 58 |
+
repetition_penalty=1.2,
|
| 59 |
+
pad_token_id=tokenizer.eos_token_id
|
| 60 |
+
)
|
| 61 |
+
output = output_ids[0][input_len:]
|
| 62 |
+
for i in range(output.shape[0]):
|
| 63 |
+
token = output[i].unsqueeze(0)
|
| 64 |
+
text_piece = tokenizer.decode(token, skip_special_tokens=True)
|
| 65 |
+
generated_text += text_piece
|
| 66 |
+
yield generated_text
|
| 67 |
|
| 68 |
+
# Gradio UI
|
|
|
|
|
|
|
|
|
|
| 69 |
demo = gr.ChatInterface(
|
| 70 |
respond,
|
| 71 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False),
|
|
|
|
|
|
|
| 72 |
additional_inputs=[
|
| 73 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
| 74 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 75 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 76 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
],
|
| 78 |
theme="Ocean",
|
| 79 |
)
|
| 80 |
|
|
|
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|