Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
-
import torch.nn.functional as F
|
5 |
from peft import (
|
6 |
LoraConfig,
|
7 |
PeftModel,
|
@@ -11,21 +10,29 @@ from peft import (
|
|
11 |
model_name = "google/gemma-2-2b-it"
|
12 |
lora_model_name="Anlam-Lab/gemma-2-2b-it-anlamlab-SA-Chatgpt4mini"
|
13 |
|
14 |
-
|
15 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="
|
17 |
-
model = PeftModel.from_pretrained(model, lora_model_name)
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
inputs = tokenizer(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
with torch.no_grad():
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
29 |
|
30 |
iface = gr.Interface(
|
31 |
fn=generate_response,
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
|
|
4 |
from peft import (
|
5 |
LoraConfig,
|
6 |
PeftModel,
|
|
|
10 |
model_name = "google/gemma-2-2b-it"
|
11 |
lora_model_name="Anlam-Lab/gemma-2-2b-it-anlamlab-SA-Chatgpt4mini"
|
12 |
|
|
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
|
|
|
15 |
|
16 |
+
model = PeftModel.from_pretrained(model, lora_model_name)
|
17 |
+
def generate_response(input_text):
|
18 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
19 |
+
|
20 |
+
generation_config = {
|
21 |
+
"max_length": 512,
|
22 |
+
"temperature": 0.01,
|
23 |
+
"do_sample": True,
|
24 |
+
"pad_token_id": tokenizer.pad_token_id,
|
25 |
+
"eos_token_id": tokenizer.eos_token_id,
|
26 |
+
}
|
27 |
+
|
28 |
with torch.no_grad():
|
29 |
+
outputs = model.generate(
|
30 |
+
**inputs,
|
31 |
+
**generation_config
|
32 |
+
)
|
33 |
+
|
34 |
+
response = tokenizer.decode(outputs[0])
|
35 |
+
return response.split("<start_of_turn>model\n")[1].split("<end_of_turn>")[0]
|
36 |
|
37 |
iface = gr.Interface(
|
38 |
fn=generate_response,
|