Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,83 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
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
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
+
import os, torch, gradio as gr, spaces
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 3 |
|
| 4 |
+
MODEL_ID = os.getenv("MODEL_ID", "JDhruv14/Gita-FT-v2-Qwen2.5-3B")
|
| 5 |
|
| 6 |
+
# Load once (CPU until first call; device_map will move to GPU on first run)
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
MODEL_ID,
|
| 10 |
+
device_map="auto",
|
| 11 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else "auto",
|
| 12 |
+
trust_remote_code=True,
|
| 13 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def _msgs_from_history(history, system_text):
|
| 16 |
+
msgs = []
|
| 17 |
+
if system_text:
|
| 18 |
+
msgs.append({"role": "system", "content": system_text})
|
| 19 |
+
for user, assistant in history:
|
| 20 |
+
if user:
|
| 21 |
+
msgs.append({"role": "user", "content": user})
|
| 22 |
+
if assistant:
|
| 23 |
+
msgs.append({"role": "assistant", "content": assistant})
|
| 24 |
+
return msgs
|
| 25 |
|
| 26 |
+
def _eos_ids(tok):
|
| 27 |
+
ids = {tok.eos_token_id}
|
| 28 |
+
im_end = tok.convert_tokens_to_ids("<|im_end|>")
|
| 29 |
+
if im_end is not None:
|
| 30 |
+
ids.add(im_end)
|
| 31 |
+
return list(ids)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
@spaces.GPU(duration=120) # REQUIRED for ZeroGPU; remove if using standard GPU hardware
|
| 34 |
+
def chat_fn(message, history, system_text, temperature, top_p, max_new, min_new):
|
| 35 |
+
msgs = _msgs_from_history(history, system_text) + [{"role": "user", "content": message}]
|
| 36 |
+
prompt = tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
|
| 37 |
+
inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
|
| 38 |
|
| 39 |
+
gen_cfg = GenerationConfig(
|
| 40 |
+
do_sample=True,
|
| 41 |
+
temperature=float(temperature),
|
| 42 |
+
top_p=float(top_p),
|
| 43 |
+
max_new_tokens=int(max_new),
|
| 44 |
+
min_new_tokens=int(min_new),
|
| 45 |
+
repetition_penalty=1.02,
|
| 46 |
+
no_repeat_ngram_size=3,
|
| 47 |
+
eos_token_id=_eos_ids(tokenizer),
|
| 48 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 49 |
+
)
|
| 50 |
+
with torch.no_grad():
|
| 51 |
+
out = model.generate(**inputs, generation_config=gen_cfg)
|
| 52 |
|
| 53 |
+
# slice off the prompt so we show only the assistant reply
|
| 54 |
+
new_tokens = out[:, inputs["input_ids"].shape[1]:]
|
| 55 |
+
reply = tokenizer.batch_decode(new_tokens, skip_special_tokens=True)[0].strip()
|
| 56 |
+
return reply
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
+
gr.Markdown(
|
| 60 |
+
"<h1 style='text-align:center'>Gita Assistant (Qwen2.5-3B Fine-tuned)</h1>"
|
| 61 |
+
"<p style='text-align:center'>Ask in English / हिंदी / ગુજરાતી. The assistant cites verses when relevant.</p>"
|
| 62 |
+
)
|
| 63 |
+
system_box = gr.Textbox(
|
| 64 |
+
value="Reply in the user’s language with 2–3 concise points (200–400 words); cite Gita verses when relevant.",
|
| 65 |
+
label="System prompt",
|
| 66 |
+
)
|
| 67 |
+
temperature = gr.Slider(0.1, 1.2, value=0.7, step=0.05, label="temperature")
|
| 68 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="top_p")
|
| 69 |
+
max_new = gr.Slider(64, 1024, value=512, step=16, label="max_new_tokens")
|
| 70 |
+
min_new = gr.Slider(0, 512, value=160, step=8, label="min_new_tokens")
|
| 71 |
|
| 72 |
+
chat = gr.ChatInterface(
|
| 73 |
+
fn=lambda m, h: chat_fn(m, h, system_box.value, temperature.value, top_p.value, max_new.value, min_new.value),
|
| 74 |
+
title=None,
|
| 75 |
+
additional_inputs=[system_box, temperature, top_p, max_new, min_new],
|
| 76 |
+
retry_btn="Regenerate",
|
| 77 |
+
undo_btn="Undo Last",
|
| 78 |
+
clear_btn="Clear",
|
| 79 |
+
queue=True, # queue is recommended (and required for ZeroGPU concurrency)
|
| 80 |
+
)
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
demo.launch()
|