omeryentur commited on
Commit
25869c0
·
verified ·
1 Parent(s): 36ac785

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
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="cpu", torch_dtype=torch.float16)
17
- model = PeftModel.from_pretrained(model, lora_model_name)
18
 
19
- def generate_response(text):
20
- example = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>Bir duygu analisti olarak sana verilen metinleri analiz et ve aşağıdaki kategorilerden yalnızca birini seçerek metnin duygu durumunu belirle:Positive,Negative,Neutral<|eot_id|><|start_header_id|>user<|end_header_id|>{text}<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
21
- inputs = tokenizer(example, return_tensors="pt")
 
 
 
 
 
 
 
 
 
22
  with torch.no_grad():
23
- model_output = model(**inputs)
24
- logits = model_output.logits
25
- probabilities = F.softmax(logits, dim=-1)
26
- top_probs, top_tokens = torch.topk(probabilities[0, -1, :], k=10)
27
- predicted_label = tokenizer.decode(top_tokens[0])
28
- return predicted_label
 
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,