Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
-
|
| 4 |
-
import torch
|
| 5 |
from transformers import AutoTokenizer, pipeline
|
| 6 |
-
from peft import AutoPeftModelForCausalLM
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
BASE_MODEL = "cjvt/GaMS-1B-Chat"
|
| 11 |
-
ADAPTER_ID = "janajankovic/autotrain-juhh6-uwiv9"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
ADAPTER_ID,
|
| 19 |
-
torch_dtype=torch.float32, # CPU in this Space
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
"text-generation",
|
| 24 |
model=model,
|
| 25 |
tokenizer=tokenizer,
|
| 26 |
max_new_tokens=256,
|
| 27 |
do_sample=True,
|
| 28 |
-
top_p=0.9,
|
| 29 |
temperature=0.7,
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
text
|
| 39 |
-
|
| 40 |
-
out = pipe(text)[0]["generated_text"]
|
| 41 |
-
reply = out.split("Model:")[-1].strip()
|
| 42 |
-
history.append((user_input, reply))
|
| 43 |
-
return history, ""
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
msg = gr.Textbox(label="Vnos")
|
| 49 |
-
clear = gr.Button("Počisti")
|
| 50 |
|
| 51 |
-
def user_send(message, chat_history):
|
| 52 |
-
chat_history = chat_history or []
|
| 53 |
-
return "", chat_fn(chat_history, message)[0]
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, pipeline
|
| 3 |
+
from peft import AutoPeftModelForCausalLM
|
|
|
|
| 4 |
|
| 5 |
+
# HF repo of your LoRA-finetuned model (the one AutoTrain pushed)
|
| 6 |
+
FINETUNED_MODEL_ID = "janajankovic/autotrain-juhh6-uwiv9" # <<< CHANGE THIS TO YOUR REPO ID
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load base+LoRA via PEFT
|
| 10 |
+
model = AutoPeftModelForCausalLM.from_pretrained(FINETUNED_MODEL_ID)
|
| 11 |
+
base_model_id = model.config.base_model_name_or_path
|
| 12 |
|
| 13 |
+
# Use tokenizer from the base model (GaMS-1B-Chat)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id)
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Text generation pipeline
|
| 17 |
+
text_gen = pipeline(
|
| 18 |
"text-generation",
|
| 19 |
model=model,
|
| 20 |
tokenizer=tokenizer,
|
| 21 |
max_new_tokens=256,
|
| 22 |
do_sample=True,
|
|
|
|
| 23 |
temperature=0.7,
|
| 24 |
+
top_p=0.9,
|
| 25 |
)
|
| 26 |
|
| 27 |
|
| 28 |
+
def respond(message, history):
|
| 29 |
+
# message: current user message (string)
|
| 30 |
+
# history: list of [user, assistant] pairs (ignored here, minimal chat)
|
| 31 |
+
prompt = message
|
| 32 |
+
outputs = text_gen(prompt, num_return_sequences=1)
|
| 33 |
+
text = outputs[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Many causal LM heads echo the prompt; strip it out if present
|
| 36 |
+
if text.startswith(prompt):
|
| 37 |
+
text = text[len(prompt):].lstrip()
|
| 38 |
|
| 39 |
+
# ChatInterface expects a plain string here
|
| 40 |
+
return text
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
demo = gr.ChatInterface(
|
| 44 |
+
fn=respond,
|
| 45 |
+
title="GenUI – Slovene fine-tuned chat",
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|