janajankovic commited on
Commit
3adcdc9
·
verified ·
1 Parent(s): 77e8483

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -37
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 # comes from `peft`
7
-
8
 
 
 
9
 
10
- BASE_MODEL = "cjvt/GaMS-1B-Chat"
11
- ADAPTER_ID = "janajankovic/autotrain-juhh6-uwiv9"
12
 
13
- # Tokenizer from the base model
14
- tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
 
15
 
16
- # Load base+adapter via AutoPeftModel
17
- model = AutoPeftModelForCausalLM.from_pretrained(
18
- ADAPTER_ID,
19
- torch_dtype=torch.float32, # CPU in this Space
20
- )
21
 
22
- pipe = pipeline(
 
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 chat_fn(history, user_input):
34
- # simple prompt: previous turns concatenated
35
- text = ""
36
- for turn in history:
37
- text += f"Uporabnik: {turn[0]}\nModel: {turn[1]}\n"
38
- text += f"Uporabnik: {user_input}\nModel:"
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
- with gr.Blocks() as demo:
47
- chatbot = gr.Chatbot(label="Slovenski finetune")
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
- msg.submit(user_send, [msg, chatbot], [msg, chatbot])
56
- clear.click(lambda: ([], ""), None, [chatbot, msg], queue=False)
 
 
57
 
58
- demo.launch()
 
 
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()