omeryentur commited on
Commit
2a9ec14
·
verified ·
1 Parent(s): ce7ef81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -43
app.py CHANGED
@@ -1,49 +1,90 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
- from peft import (
5
- LoraConfig,
6
- PeftModel,
7
- prepare_model_for_kbit_training,
8
- get_peft_model,
9
- )
10
- model_name = "meta-llama/Llama-3.2-1B"
11
- lora_model_name="Anlam-Lab/Llama-3.2-1B-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("<|end_header_id|>")[-1].split("<|eot_id|>")[0]
36
-
37
- iface = gr.Interface(
38
- fn=generate_response,
39
- inputs=gr.Textbox(lines=5, placeholder="Metninizi buraya girin..."),
40
- outputs=gr.Textbox(lines=5, label="Model Çıktısı"),
41
- title="Anlam-Lab",
42
- examples=[
43
- ["Akıllı saati uzun süre kullandım ve şık tasarımı, harika sağlık takibi özellikleri ve uzun pil ömrüyle çok memnun kaldım."],
44
- ["Ürünü aldım ama pil ömrü kısa, ekran parlaklığı yetersiz ve sağlık takibi doğru sonuçlar vermedi."],
45
- ]
46
- )
47
 
48
  if __name__ == "__main__":
49
- iface.launch()
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
+ from peft import PeftModel
5
+ from typing import Dict, Any
6
+
7
+ class LlamaInterface:
8
+ def __init__(
9
+ self,
10
+ base_model_name: str = "meta-llama/Llama-3.2-1B",
11
+ lora_model_name: str = "Anlam-Lab/Llama-3.2-1B-it-anlamlab-SA-Chatgpt4mini"
12
+ ):
13
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
14
+
15
+ self.tokenizer = AutoTokenizer.from_pretrained(base_model_name)
16
+ self.model = AutoModelForCausalLM.from_pretrained(
17
+ base_model_name,
18
+ device_map="auto",
19
+ torch_dtype=torch.float16
20
+ )
21
+ self.model = PeftModel.from_pretrained(self.model, lora_model_name)
22
+ self.model.eval()
23
+
24
+ def generate_response(self, input_text: str) -> str:
25
+ if not input_text or not input_text.strip():
26
+ return "Error: Please provide valid input text."
27
+
28
+ try:
29
+ inputs = self.tokenizer(
30
+ input_text,
31
+ return_tensors="pt",
32
+ padding=True,
33
+ truncation=True,
34
+ max_length=512
35
+ ).to(self.device)
36
+
37
+ generation_config: Dict[str, Any] = {
38
+ "max_length": 512,
39
+ "temperature": 0.01,
40
+ "do_sample": True,
41
+ "top_k": 2,
42
+ "top_p": 0.95,
43
+ }
44
+
45
+ with torch.no_grad():
46
+ outputs = self.model.generate(**inputs, **generation_config)
47
+
48
+ response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
49
+ return response.split("<|end_header_id|>")[-1].split("<|eot_id|>")[0].strip()
50
+
51
+ except Exception as e:
52
+ return f"Error generating response: {str(e)}"
53
+
54
+ def create_interface(self) -> gr.Interface:
55
+ return gr.Interface(
56
+ fn=self.generate_response,
57
+ inputs=gr.Textbox(
58
+ lines=5,
59
+ placeholder="Metninizi buraya girin...",
60
+ label="Giriş Metni"
61
+ ),
62
+ outputs=gr.Textbox(
63
+ lines=5,
64
+ label="Model Çıktısı"
65
+ ),
66
+ title="Anlam-Lab Duygu Analizi",
67
+ description="Metin girişi yaparak duygu analizi sonucunu alabilirsiniz.",
68
+ examples=[
69
+ ["Akıllı saati uzun süre kullandım ve şık tasarımı, harika sağlık takibi özellikleri ve uzun pil ömrüyle çok memnun kaldım."],
70
+ ["Ürünü aldım ama pil ömrü kısa, ekran parlaklığı yetersiz ve sağlık takibi doğru sonuçlar vermedi."],
71
+ ],
72
+ theme="default"
73
+ )
74
+
75
+ def main():
76
+ try:
77
+ llama_interface = LlamaInterface()
78
+ interface = llama_interface.create_interface()
79
+ interface.launch(
80
+ share=False,
81
+ debug=True,
82
+ server_name="0.0.0.0",
83
+ server_port=7860
84
  )
85
+ except Exception as e:
86
+ print(f"Error launching interface: {str(e)}")
87
+ raise
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  if __name__ == "__main__":
90
+ main()