File size: 6,039 Bytes
ad32177
 
cef31a4
 
 
ad32177
 
cef31a4
ad32177
 
 
 
8511f5e
cef31a4
 
 
 
8511f5e
cef31a4
 
8511f5e
cef31a4
8511f5e
 
cef31a4
 
8511f5e
 
cef31a4
 
 
 
 
 
 
8511f5e
cef31a4
 
 
8511f5e
cef31a4
8511f5e
 
cef31a4
8511f5e
cef31a4
8511f5e
 
cef31a4
8511f5e
cef31a4
8511f5e
cef31a4
8511f5e
 
 
cef31a4
 
8511f5e
 
cef31a4
8511f5e
 
40bbb95
8511f5e
 
cef31a4
8511f5e
 
 
 
40bbb95
8511f5e
 
 
40bbb95
cef31a4
8511f5e
ad32177
40bbb95
8511f5e
 
40bbb95
8511f5e
 
 
 
 
 
 
cef31a4
8511f5e
cef31a4
 
 
8511f5e
cef31a4
 
 
 
 
8511f5e
 
cef31a4
 
8511f5e
 
 
cef31a4
8511f5e
 
40bbb95
 
8511f5e
 
 
cef31a4
8511f5e
 
 
 
 
 
cef31a4
8511f5e
40bbb95
8511f5e
cef31a4
8511f5e
40bbb95
ad32177
8511f5e
 
 
 
 
5b97012
ad32177
cef31a4
8511f5e
ad32177
8511f5e
 
ad32177
8511f5e
ad32177
 
8511f5e
cef31a4
8511f5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cef31a4
8511f5e
 
cef31a4
8511f5e
 
 
 
 
5b97012
ad32177
cef31a4
 
8511f5e
 
 
 
 
cef31a4
8511f5e
cef31a4
8511f5e
 
 
ad32177
 
8511f5e
ad32177
8511f5e
5b97012
8511f5e
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import gradio as gr
import os
import time
from collections import defaultdict
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch

# Load secrets from environment
HF_TOKEN = os.getenv("HF_TOKEN")
API_KEY = os.getenv("API_KEY")
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")

print("πŸ” Security Status:")
print(f"   HF_TOKEN: {'βœ… Set' if HF_TOKEN else '❌ Not set'}")
print(f"   API_KEY: {'βœ… Set' if API_KEY else '❌ Not set'}")
print(f"   ADMIN_PASSWORD: {'βœ… Set' if ADMIN_PASSWORD else '❌ Not set'}")

# Simple rate limiting
request_counts = defaultdict(list)

# Load model
model_name = "gpt2"
print("πŸ“¦ Loading model...")

try:
    if HF_TOKEN:
        tokenizer = GPT2Tokenizer.from_pretrained(model_name, token=HF_TOKEN)
        model = GPT2LMHeadModel.from_pretrained(model_name, token=HF_TOKEN)
        print("βœ… Model loaded with HF token")
    else:
        tokenizer = GPT2Tokenizer.from_pretrained(model_name)
        model = GPT2LMHeadModel.from_pretrained(model_name)
        print("βœ… Model loaded without token")
        
    tokenizer.pad_token = tokenizer.eos_token
    print("βœ… Model ready!")
    
except Exception as e:
    print(f"❌ Model loading failed: {e}")
    raise

def check_api_key(provided_key):
    """Simple API key validation with rate limiting"""
    if not API_KEY:
        return True, "Public access"
    
    if not provided_key or provided_key != API_KEY:
        return False, "Invalid or missing API key"
    
    # Simple rate limiting (100 requests per hour)
    now = time.time()
    hour_ago = now - 3600
    
    # Clean old requests
    request_counts[provided_key] = [
        t for t in request_counts[provided_key] if t > hour_ago
    ]
    
    if len(request_counts[provided_key]) >= 100:
        return False, "Rate limit exceeded (100/hour)"
    
    request_counts[provided_key].append(now)
    return True, f"Authenticated ({len(request_counts[provided_key])}/100)"

def generate_text(prompt, max_length, temperature, top_p, top_k, api_key):
    """Generate text with GPT-2"""
    
    # API key check
    valid, msg = check_api_key(api_key)
    if not valid:
        return f"πŸ”’ Error: {msg}"
    
    # Input validation  
    if not prompt.strip():
        return "❌ Please enter a prompt"
    
    if len(prompt) > 1000:
        return "❌ Prompt too long (max 1000 chars)"
    
    try:
        print(f"πŸ”‘ {msg}")
        print(f"πŸ“ Generating: {prompt[:50]}...")
        
        # Encode input
        inputs = tokenizer.encode(
            prompt, 
            return_tensors="pt", 
            max_length=400, 
            truncation=True
        )
        
        # Generate
        with torch.no_grad():
            outputs = model.generate(
                inputs,
                max_length=min(inputs.shape[1] + max_length, 500),
                temperature=max(0.1, min(2.0, temperature)),
                top_p=max(0.1, min(1.0, top_p)),
                top_k=max(1, min(100, top_k)),
                do_sample=True,
                pad_token_id=tokenizer.eos_token_id,
                num_return_sequences=1,
                no_repeat_ngram_size=2
            )
        
        # Decode result
        generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
        result = generated[len(prompt):].strip()
        
        print(f"βœ… Generated {len(result)} characters")
        return result if result else "❌ No text generated"
        
    except Exception as e:
        error = f"❌ Generation failed: {str(e)}"
        print(error)
        return error

# Create simple interface - NO COMPLEX THEMES OR CSS
demo = gr.Blocks(title="GPT-2 Text Generator")

with demo:
    # Simple header
    gr.Markdown("# πŸ€– GPT-2 Text Generator")
    
    # Security info
    if API_KEY:
        gr.Markdown("πŸ”’ **API Authentication Required**")
    else:
        gr.Markdown("πŸ”“ **Public Access Mode**")
    
    with gr.Row():
        with gr.Column():
            # Input section
            prompt = gr.Textbox(
                label="Prompt", 
                placeholder="Enter your text prompt...",
                lines=3
            )
            
            # API key input (only if needed)
            if API_KEY:
                api_key = gr.Textbox(
                    label="API Key",
                    type="password",
                    placeholder="Enter API key..."
                )
            else:
                api_key = gr.Textbox(value="", visible=False)
            
            # Parameters
            max_length = gr.Slider(
                10, 200, 100, 
                label="Max Length"
            )
            temperature = gr.Slider(
                0.1, 2.0, 0.7, 
                label="Temperature"
            )
            top_p = gr.Slider(
                0.1, 1.0, 0.9, 
                label="Top-p"
            )
            top_k = gr.Slider(
                1, 100, 50, 
                label="Top-k"
            )
            
            # Generate button
            generate_btn = gr.Button("Generate", variant="primary")
        
        with gr.Column():
            # Output
            output = gr.Textbox(
                label="Generated Text",
                lines=10,
                placeholder="Generated text will appear here..."
            )
    
    # Examples
    gr.Examples([
        ["Once upon a time"],
        ["The future of AI is"],
        ["In a world where technology"],
    ], inputs=prompt)
    
    # Connect function
    generate_btn.click(
        generate_text,
        inputs=[prompt, max_length, temperature, top_p, top_k, api_key],
        outputs=output
    )

# Simple launch - MINIMAL CONFIGURATION
if __name__ == "__main__":
    auth = ("admin", ADMIN_PASSWORD) if ADMIN_PASSWORD else None
    
    if auth:
        print("πŸ” Admin auth enabled")
    
    print("πŸš€ Starting server...")
    
    # MINIMAL launch config that works on HF Spaces
    demo.launch(auth=auth)
    
    print("βœ… Server running!")