Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,358 +1,57 @@
|
|
1 |
-
|
2 |
import subprocess
|
3 |
import sys
|
4 |
-
import time
|
5 |
-
import threading
|
6 |
-
import logging
|
7 |
-
from typing import Dict, Optional, Tuple
|
8 |
-
import json
|
9 |
-
import os
|
10 |
-
from datetime import datetime, timedelta
|
11 |
-
|
12 |
-
# Configure logging
|
13 |
-
logging.basicConfig(
|
14 |
-
level=logging.INFO,
|
15 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
16 |
-
)
|
17 |
-
logger = logging.getLogger(__name__)
|
18 |
|
19 |
-
# Dynamic installation of required packages
|
20 |
def install_and_import(package):
|
21 |
try:
|
22 |
__import__(package)
|
23 |
except ImportError:
|
24 |
-
|
25 |
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
26 |
|
27 |
-
#
|
28 |
install_and_import("gradio")
|
29 |
install_and_import("transformers")
|
30 |
install_and_import("torch")
|
31 |
|
|
|
32 |
import gradio as gr
|
33 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
34 |
import torch
|
35 |
|
36 |
-
#
|
37 |
-
class RateLimiter:
|
38 |
-
def __init__(self, config: Dict[str, Dict[str, int]]):
|
39 |
-
self.config = config
|
40 |
-
self.user_data = {} # {ip: {tier: {'count': x, 'window_start': timestamp}}}
|
41 |
-
self.lock = threading.Lock()
|
42 |
-
|
43 |
-
def check_and_record_request(self, identifier: str) -> Tuple[bool, Optional[str], Optional[Dict]]:
|
44 |
-
"""Check if request is allowed and record it"""
|
45 |
-
with self.lock:
|
46 |
-
current_time = time.time()
|
47 |
-
|
48 |
-
# Initialize user data if needed
|
49 |
-
if identifier not in self.user_data:
|
50 |
-
self.user_data[identifier] = {}
|
51 |
-
|
52 |
-
# Check each tier
|
53 |
-
for tier_name, tier_config in self.config.items():
|
54 |
-
window_seconds = tier_config['window']
|
55 |
-
limit = tier_config['limit']
|
56 |
-
|
57 |
-
# Initialize tier data if needed
|
58 |
-
if tier_name not in self.user_data[identifier]:
|
59 |
-
self.user_data[identifier][tier_name] = {
|
60 |
-
'count': 0,
|
61 |
-
'window_start': current_time
|
62 |
-
}
|
63 |
-
|
64 |
-
tier_data = self.user_data[identifier][tier_name]
|
65 |
-
|
66 |
-
# Check if current window has expired
|
67 |
-
window_elapsed = current_time - tier_data['window_start']
|
68 |
-
|
69 |
-
if window_elapsed >= window_seconds:
|
70 |
-
# Window expired, reset
|
71 |
-
tier_data['count'] = 0
|
72 |
-
tier_data['window_start'] = current_time
|
73 |
-
logger.info(f"Window reset for {identifier} - {tier_name}")
|
74 |
-
|
75 |
-
# Check if limit reached
|
76 |
-
if tier_data['count'] >= limit:
|
77 |
-
wait_time = window_seconds - window_elapsed
|
78 |
-
|
79 |
-
error_msg = (
|
80 |
-
f"Rate limit aşıldı ({tier_name}): "
|
81 |
-
f"{tier_data['count']}/{limit} kullanıldı. "
|
82 |
-
f"Bekleme süresi: {wait_time:.0f} saniye"
|
83 |
-
)
|
84 |
-
|
85 |
-
stats = self._get_all_stats(identifier, current_time)
|
86 |
-
return False, error_msg, stats
|
87 |
-
|
88 |
-
# Request allowed - increment all counters
|
89 |
-
for tier_name in self.config:
|
90 |
-
self.user_data[identifier][tier_name]['count'] += 1
|
91 |
-
|
92 |
-
stats = self._get_all_stats(identifier, current_time)
|
93 |
-
logger.info(f"Request allowed for {identifier}. Stats: {stats}")
|
94 |
-
|
95 |
-
return True, None, stats
|
96 |
-
|
97 |
-
def _get_all_stats(self, identifier: str, current_time: float) -> Dict:
|
98 |
-
"""Get usage statistics for all tiers"""
|
99 |
-
stats = {}
|
100 |
-
|
101 |
-
for tier_name, tier_config in self.config.items():
|
102 |
-
window_seconds = tier_config['window']
|
103 |
-
limit = tier_config['limit']
|
104 |
-
|
105 |
-
if identifier in self.user_data and tier_name in self.user_data[identifier]:
|
106 |
-
tier_data = self.user_data[identifier][tier_name]
|
107 |
-
window_elapsed = current_time - tier_data['window_start']
|
108 |
-
|
109 |
-
# If window expired, show as reset
|
110 |
-
if window_elapsed >= window_seconds:
|
111 |
-
used = 0
|
112 |
-
reset_in = 0
|
113 |
-
else:
|
114 |
-
used = tier_data['count']
|
115 |
-
reset_in = window_seconds - window_elapsed
|
116 |
-
else:
|
117 |
-
used = 0
|
118 |
-
reset_in = 0
|
119 |
-
|
120 |
-
stats[tier_name] = {
|
121 |
-
'used': used,
|
122 |
-
'limit': limit,
|
123 |
-
'remaining': max(0, limit - used),
|
124 |
-
'reset_in': reset_in,
|
125 |
-
'window_text': self._format_window(window_seconds)
|
126 |
-
}
|
127 |
-
|
128 |
-
return stats
|
129 |
-
|
130 |
-
def _format_window(self, seconds: int) -> str:
|
131 |
-
"""Format time window in Turkish"""
|
132 |
-
if seconds < 60:
|
133 |
-
return f"{seconds} saniye"
|
134 |
-
elif seconds < 3600:
|
135 |
-
return f"{seconds // 60} dakika"
|
136 |
-
elif seconds < 86400:
|
137 |
-
return f"{seconds // 3600} saat"
|
138 |
-
else:
|
139 |
-
return f"{seconds // 86400} gün"
|
140 |
-
|
141 |
-
# Initialize model
|
142 |
-
logger.info("Model yükleniyor...")
|
143 |
model_name = "Bertug1911/BrtGPT-124m-Base"
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
except Exception as e:
|
156 |
-
logger.error(f"Model yükleme hatası: {e}")
|
157 |
-
raise
|
158 |
-
|
159 |
-
# Rate limiting configuration
|
160 |
-
RATE_LIMIT_CONFIG = {
|
161 |
-
'dakika': {'window': 60, 'limit': 5}, # 5 istek / dakika
|
162 |
-
'saat': {'window': 3600, 'limit': 50}, # 50 istek / saat
|
163 |
-
'gün': {'window': 86400, 'limit': 500}, # 500 istek / gün
|
164 |
-
}
|
165 |
-
|
166 |
-
# Initialize rate limiter
|
167 |
-
rate_limiter = RateLimiter(RATE_LIMIT_CONFIG)
|
168 |
-
|
169 |
-
# Generation function
|
170 |
-
def generate_text_with_rate_limit(
|
171 |
-
prompt: str,
|
172 |
-
temperature: float,
|
173 |
-
top_k: int,
|
174 |
-
max_new_tokens: int,
|
175 |
-
request: gr.Request
|
176 |
-
) -> str:
|
177 |
-
"""Generate text with rate limiting"""
|
178 |
-
|
179 |
-
# Get client IP
|
180 |
-
ip = request.client.host if request and request.client else "unknown"
|
181 |
-
|
182 |
-
# Check rate limit
|
183 |
-
is_allowed, error_msg, stats = rate_limiter.check_and_record_request(ip)
|
184 |
-
|
185 |
-
if not is_allowed:
|
186 |
-
error_output = f"❌ **{error_msg}**\n\n"
|
187 |
-
error_output += "📊 **Kredi Durumu:**\n\n"
|
188 |
-
|
189 |
-
for tier, info in stats.items():
|
190 |
-
percentage = (info['remaining'] / info['limit']) * 100 if info['limit'] > 0 else 0
|
191 |
-
|
192 |
-
if percentage > 50:
|
193 |
-
bar = "🟩" * 5
|
194 |
-
elif percentage > 20:
|
195 |
-
bar = "🟨" * 3 + "⬜" * 2
|
196 |
-
else:
|
197 |
-
bar = "🟥" * 1 + "⬜" * 4
|
198 |
-
|
199 |
-
error_output += f"**{tier.capitalize()}** [{bar}]\n"
|
200 |
-
error_output += f"├─ Kullanılan: {info['used']}/{info['limit']}\n"
|
201 |
-
error_output += f"├─ Kalan: {info['remaining']}\n"
|
202 |
-
error_output += f"└─ Yenileme: {info['reset_in']:.0f} saniye\n\n"
|
203 |
-
|
204 |
-
return error_output
|
205 |
-
|
206 |
-
try:
|
207 |
-
# Input validation
|
208 |
-
if not prompt or len(prompt.strip()) == 0:
|
209 |
-
return "⚠️ Lütfen bir prompt girin."
|
210 |
-
|
211 |
-
if len(prompt) > 1000:
|
212 |
-
return "⚠️ Prompt çok uzun. Maksimum 1000 karakter."
|
213 |
-
|
214 |
-
# Generate text
|
215 |
-
logger.info(f"Metin oluşturuluyor: temp={temperature}, top_k={top_k}, max_tokens={max_new_tokens}")
|
216 |
-
|
217 |
-
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
218 |
-
if torch.cuda.is_available():
|
219 |
-
inputs = inputs.to("cuda")
|
220 |
-
|
221 |
-
with torch.no_grad():
|
222 |
-
output = model.generate(
|
223 |
-
**inputs,
|
224 |
-
max_new_tokens=int(max_new_tokens),
|
225 |
-
temperature=float(temperature),
|
226 |
-
top_k=int(top_k),
|
227 |
-
do_sample=True,
|
228 |
-
pad_token_id=tokenizer.eos_token_id
|
229 |
-
)
|
230 |
-
|
231 |
-
generated_text = tokenizer.decode(output[0], skip_special_tokens=False)
|
232 |
-
generated_text = generated_text.replace("Ġ", " ")
|
233 |
-
|
234 |
-
# Add credit status
|
235 |
-
footer = "\n\n" + "─" * 50 + "\n"
|
236 |
-
footer += "💳 **Kredi Durumu:**\n\n"
|
237 |
-
|
238 |
-
for tier, info in stats.items():
|
239 |
-
percentage = (info['remaining'] / info['limit']) * 100 if info['limit'] > 0 else 0
|
240 |
-
|
241 |
-
if percentage > 50:
|
242 |
-
emoji = "✅"
|
243 |
-
elif percentage > 20:
|
244 |
-
emoji = "⚠️"
|
245 |
-
else:
|
246 |
-
emoji = "🔴"
|
247 |
-
|
248 |
-
footer += f"{emoji} **{tier.capitalize()}**: "
|
249 |
-
footer += f"{info['used']}/{info['limit']} kullanıldı "
|
250 |
-
footer += f"({info['remaining']} kaldı)"
|
251 |
-
|
252 |
-
if info['reset_in'] > 0:
|
253 |
-
minutes = int(info['reset_in'] // 60)
|
254 |
-
seconds = int(info['reset_in'] % 60)
|
255 |
-
if minutes > 0:
|
256 |
-
footer += f" - Yenileme: {minutes}dk {seconds}sn"
|
257 |
-
else:
|
258 |
-
footer += f" - Yenileme: {seconds} saniye"
|
259 |
-
footer += "\n"
|
260 |
-
|
261 |
-
return generated_text + footer
|
262 |
-
|
263 |
-
except Exception as e:
|
264 |
-
logger.error(f"Metin oluşturma hatası: {e}", exc_info=True)
|
265 |
-
return f"❌ Hata: {str(e)}"
|
266 |
-
|
267 |
-
# Gradio interface
|
268 |
-
with gr.Blocks(title="BrtGPT-124m-Base", theme=gr.themes.Soft()) as app:
|
269 |
-
gr.Markdown("""
|
270 |
-
# 🤖 BrtGPT-124m-Base Metin Üreteci
|
271 |
-
|
272 |
-
### 📋 Kredi Sistemi (Fixed Window)
|
273 |
-
- **Dakika**: 5 istek / 60 saniye
|
274 |
-
- **Saat**: 50 istek / 3600 saniye
|
275 |
-
- **Gün**: 500 istek / 86400 saniye
|
276 |
-
|
277 |
-
⚠️ **Not**: Her kategori kendi pencere süresine göre sıfırlanır.
|
278 |
-
İlk kullanımdan itibaren süre başlar.
|
279 |
-
""")
|
280 |
-
|
281 |
-
with gr.Row():
|
282 |
-
with gr.Column(scale=2):
|
283 |
-
prompt_input = gr.Textbox(
|
284 |
-
lines=5,
|
285 |
-
placeholder="Metin üretmek için prompt'unuzu buraya yazın...",
|
286 |
-
label="📝 Prompt",
|
287 |
-
max_lines=10
|
288 |
-
)
|
289 |
-
|
290 |
-
with gr.Row():
|
291 |
-
temperature_slider = gr.Slider(
|
292 |
-
minimum=0.01,
|
293 |
-
maximum=1.0,
|
294 |
-
value=0.5,
|
295 |
-
step=0.01,
|
296 |
-
label="🌡️ Temperature (Yaratıcılık)"
|
297 |
-
)
|
298 |
-
top_k_slider = gr.Slider(
|
299 |
-
minimum=1,
|
300 |
-
maximum=50,
|
301 |
-
value=10,
|
302 |
-
step=1,
|
303 |
-
label="🎯 Top-K (Çeşitlilik)"
|
304 |
-
)
|
305 |
-
max_tokens_slider = gr.Slider(
|
306 |
-
minimum=1,
|
307 |
-
maximum=100,
|
308 |
-
value=30,
|
309 |
-
step=1,
|
310 |
-
label="📏 Maksimum Token"
|
311 |
-
)
|
312 |
-
|
313 |
-
generate_button = gr.Button("🚀 Metin Oluştur", variant="primary", size="lg")
|
314 |
-
|
315 |
-
with gr.Column(scale=3):
|
316 |
-
output_text = gr.Textbox(
|
317 |
-
label="📄 Oluşturulan Metin",
|
318 |
-
lines=15,
|
319 |
-
max_lines=25,
|
320 |
-
interactive=False,
|
321 |
-
show_copy_button=True
|
322 |
-
)
|
323 |
-
|
324 |
-
gr.Markdown("""
|
325 |
-
---
|
326 |
-
### ℹ️ Sistem Bilgisi
|
327 |
-
- Toplam 3 paralel instance mevcut
|
328 |
-
- Şu an **Instance 1**'desiniz
|
329 |
-
- Yoğunluk durumunda diğer instance'ları deneyin
|
330 |
-
|
331 |
-
📧 **İletişim**: bertug2099@gmail.com | bertugscpmail@gmail.com
|
332 |
-
""")
|
333 |
-
|
334 |
-
# Event handler
|
335 |
-
generate_button.click(
|
336 |
-
fn=generate_text_with_rate_limit,
|
337 |
-
inputs=[prompt_input, temperature_slider, top_k_slider, max_tokens_slider],
|
338 |
-
outputs=output_text
|
339 |
-
)
|
340 |
-
|
341 |
-
# Examples
|
342 |
-
gr.Examples(
|
343 |
-
examples=[
|
344 |
-
["Teknolojinin gelecekte hayatımızı nasıl değiştireceğini", 0.7, 15, 50],
|
345 |
-
["Gizemli bir ormanın derinliklerinde", 0.5, 10, 40],
|
346 |
-
["Uzay yolculuğu ve insanlığın geleceği", 0.6, 20, 60],
|
347 |
-
],
|
348 |
-
inputs=[prompt_input, temperature_slider, top_k_slider, max_tokens_slider],
|
349 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
350 |
|
351 |
-
|
352 |
-
logger.info("Uygulama başlatılıyor...")
|
353 |
-
app.launch(
|
354 |
-
server_name="0.0.0.0",
|
355 |
-
server_port=7860,
|
356 |
-
share=False,
|
357 |
-
show_error=True
|
358 |
-
)
|
|
|
|
|
1 |
import subprocess
|
2 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
4 |
def install_and_import(package):
|
5 |
try:
|
6 |
__import__(package)
|
7 |
except ImportError:
|
8 |
+
print(f"{package} is not installed, installing...")
|
9 |
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
10 |
|
11 |
+
# Gerekli paketleri kontrol et ve kur
|
12 |
install_and_import("gradio")
|
13 |
install_and_import("transformers")
|
14 |
install_and_import("torch")
|
15 |
|
16 |
+
# Şimdi import et
|
17 |
import gradio as gr
|
18 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
19 |
import torch
|
20 |
|
21 |
+
# Model ve tokenizer yükleme 'yeni
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
model_name = "Bertug1911/BrtGPT-124m-Base"
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
25 |
+
|
26 |
+
def generate_text(prompt, temperature, top_k, max_new_tokens):
|
27 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
28 |
+
output = model.generate(
|
29 |
+
**inputs,
|
30 |
+
max_new_tokens=int(max_new_tokens),
|
31 |
+
temperature=float(temperature),
|
32 |
+
top_k=int(top_k),
|
33 |
+
do_sample=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
)
|
35 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=False)
|
36 |
+
generated_text = generated_text.replace(" ", "").replace("Ġ", " ")
|
37 |
+
return generated_text
|
38 |
+
|
39 |
+
arayuz = gr.Interface(
|
40 |
+
fn=generate_text,
|
41 |
+
inputs=[
|
42 |
+
gr.Textbox(lines=3, placeholder="Your prompt..."),
|
43 |
+
gr.Slider(minimum=0.01, maximum=1.0, value=0.5, step=0.01, label="Temperature"),
|
44 |
+
gr.Slider(minimum=1, maximum=50, value=10, step=1, label="Top-K"),
|
45 |
+
gr.Slider(minimum=1, maximum=50, value=15, step=1, label="Max New Tokens"),
|
46 |
+
],
|
47 |
+
outputs="text",
|
48 |
+
title="BrtGPT-124m-Base",
|
49 |
+
description="""
|
50 |
+
If the system/application slows down as the number of users increases, we will update the number and type of GPUs.
|
51 |
+
Also, until we update, there are 3 applications that do the same thing but are copied to distribute the workload.
|
52 |
+
"YOU ARE CURRENTLY ON APPLICATION 1"
|
53 |
+
If this application slows down too much, you can use the 2nd or 3rd application, if you think all of them are slowing down right now, contact me:
|
54 |
+
"bertug2099@gmail.com or bertugscpmail@gmail.com"""
|
55 |
+
)
|
56 |
|
57 |
+
arayuz.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|