lsgz commited on
Commit
a250d64
Β·
verified Β·
1 Parent(s): 7a7a344

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -16
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import huggingface_hub
2
 
3
  if not hasattr(huggingface_hub, "HfFolder"):
@@ -6,27 +7,114 @@ if not hasattr(huggingface_hub, "HfFolder"):
6
  def get_token():
7
  return huggingface_hub.get_token()
8
 
 
 
 
 
 
 
 
 
 
 
 
9
  huggingface_hub.HfFolder = HfFolder
10
 
 
11
  import spaces
12
- import torch
13
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- print("Torch:", torch.__version__)
16
- print("CUDA patched:", torch.cuda.is_available())
17
- print("spaces:", spaces.__file__)
 
 
18
 
19
- @spaces.GPU
20
- def test_gpu():
21
- return (
22
- f"CUDA: {torch.cuda.is_available()}\n"
23
- f"Device count: {torch.cuda.device_count()}\n"
24
- f"Device: {torch.cuda.get_device_name(0)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
 
27
- gr.Interface(
28
- fn=test_gpu,
29
- inputs=None,
30
- outputs="text",
31
- title="ZeroGPU Test"
32
- ).launch()
 
 
 
 
 
 
 
 
 
 
1
+ # Compatibility fix
2
  import huggingface_hub
3
 
4
  if not hasattr(huggingface_hub, "HfFolder"):
 
7
  def get_token():
8
  return huggingface_hub.get_token()
9
 
10
+ @staticmethod
11
+ def save_token(token):
12
+ return huggingface_hub.login(token=token)
13
+
14
+ @staticmethod
15
+ def delete_token():
16
+ try:
17
+ huggingface_hub.logout()
18
+ except Exception:
19
+ pass
20
+
21
  huggingface_hub.HfFolder = HfFolder
22
 
23
+
24
  import spaces
 
25
  import gradio as gr
26
+ import torch
27
+
28
+ from transformers import AutoTokenizer, AutoModelForCausalLM
29
+ from peft import PeftModel, PeftConfig
30
+
31
+
32
+ ADAPTER = "lsgz/lsgz-personality-clone"
33
+
34
+ # Get base model from your LoRA config
35
+ config = PeftConfig.from_pretrained(ADAPTER)
36
+ BASE_MODEL = config.base_model_name_or_path
37
+
38
+ print("Base model:", BASE_MODEL)
39
+
40
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
41
+
42
+ if tokenizer.pad_token is None:
43
+ tokenizer.pad_token = tokenizer.eos_token
44
+
45
+
46
+ # -------------------------
47
+ # LOAD MODEL ON CPU
48
+ # -------------------------
49
+
50
+ print("Loading base model...")
51
 
52
+ base_model = AutoModelForCausalLM.from_pretrained(
53
+ BASE_MODEL,
54
+ torch_dtype=torch.float16,
55
+ low_cpu_mem_usage=True,
56
+ )
57
 
58
+ print("Loading LSGZ adapter...")
59
+
60
+ model = PeftModel.from_pretrained(
61
+ base_model,
62
+ ADAPTER
63
+ )
64
+
65
+ model.eval()
66
+
67
+ print("Model ready on CPU.")
68
+
69
+
70
+ # -------------------------
71
+ # GPU INFERENCE
72
+ # -------------------------
73
+
74
+ @spaces.GPU(duration=120)
75
+ def respond(message, history):
76
+
77
+ print("GPU available:", torch.cuda.is_available())
78
+ print("GPU:", torch.cuda.get_device_name(0))
79
+
80
+ # GPU exists HERE
81
+ model.to("cuda")
82
+
83
+ inputs = tokenizer(
84
+ message,
85
+ return_tensors="pt"
86
+ ).to("cuda")
87
+
88
+ with torch.inference_mode():
89
+ outputs = model.generate(
90
+ **inputs,
91
+ max_new_tokens=200,
92
+ do_sample=True,
93
+ temperature=0.7,
94
+ top_p=0.9,
95
+ repetition_penalty=1.1,
96
+ pad_token_id=tokenizer.eos_token_id,
97
+ )
98
+
99
+ generated = outputs[0][inputs["input_ids"].shape[1]:]
100
+
101
+ response = tokenizer.decode(
102
+ generated,
103
+ skip_special_tokens=True
104
  )
105
 
106
+ return response.strip()
107
+
108
+
109
+ # -------------------------
110
+ # GRADIO
111
+ # -------------------------
112
+
113
+ demo = gr.ChatInterface(
114
+ fn=respond,
115
+ title="LSGZ Personality Clone",
116
+ description="Chat with LSGZ πŸ’¬",
117
+ )
118
+
119
+ demo.queue()
120
+ demo.launch()