File size: 11,089 Bytes
ad32177
 
 
 
 
 
 
 
40bbb95
 
 
 
 
 
ad32177
40bbb95
ad32177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40bbb95
ad32177
 
 
 
40bbb95
ad32177
 
 
 
40bbb95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad32177
 
40bbb95
 
 
 
ad32177
 
 
40bbb95
 
 
 
 
 
 
 
 
 
ad32177
40bbb95
 
 
 
 
 
 
 
 
ad32177
40bbb95
 
 
 
 
 
 
 
ad32177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40bbb95
ad32177
40bbb95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad32177
 
 
 
 
 
 
 
 
 
 
 
40bbb95
 
 
 
 
 
 
 
 
 
ad32177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40bbb95
ad32177
 
 
 
 
 
 
 
 
 
 
 
 
 
40bbb95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad32177
 
40bbb95
 
 
06e1ad9
 
 
 
40bbb95
 
 
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import gradio as gr
import os
from transformers import (
    GPT2LMHeadModel, GPT2Tokenizer,
    T5ForConditionalGeneration, T5Tokenizer,
    AutoTokenizer, AutoModelForCausalLM
)
import torch
import json
from fastapi import FastAPI, HTTPException, Depends, Header
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
import uvicorn
from pydantic import BaseModel
from typing import Optional

# Configuration for multiple models
MODEL_CONFIGS = {
    "gpt2": {
        "type": "causal",
        "model_class": GPT2LMHeadModel,
        "tokenizer_class": GPT2Tokenizer,
        "description": "Original GPT-2, good for creative writing",
        "size": "117M"
    },
    "distilgpt2": {
        "type": "causal",
        "model_class": AutoModelForCausalLM,
        "tokenizer_class": AutoTokenizer,
        "description": "Smaller, faster GPT-2",
        "size": "82M"
    },
    "google/flan-t5-small": {
        "type": "seq2seq",
        "model_class": T5ForConditionalGeneration,
        "tokenizer_class": T5Tokenizer,
        "description": "Instruction-following T5 model",
        "size": "80M"
    },
    "microsoft/DialoGPT-small": {
        "type": "causal",
        "model_class": AutoModelForCausalLM,
        "tokenizer_class": AutoTokenizer,
        "description": "Conversational AI model",
        "size": "117M"
    }
}

# Environment variables
HF_TOKEN = os.getenv("HF_TOKEN")
API_KEY = os.getenv("API_KEY")
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")

# Global state for caching
loaded_model_name = None
model = None
tokenizer = None

# Pydantic models for API
class GenerateRequest(BaseModel):
    prompt: str
    model_name: str = "gpt2"
    max_length: int = 100
    temperature: float = 0.7
    top_p: float = 0.9
    top_k: int = 50

class GenerateResponse(BaseModel):
    generated_text: str
    model_used: str
    status: str = "success"

# Security
security = HTTPBearer(auto_error=False)

def load_model_and_tokenizer(model_name):
    global loaded_model_name, model, tokenizer
    
    if model_name not in MODEL_CONFIGS:
        raise ValueError(f"Model {model_name} not supported. Available models: {list(MODEL_CONFIGS.keys())}")
    
    if model_name == loaded_model_name and model is not None and tokenizer is not None:
        return model, tokenizer
    
    try:
        config = MODEL_CONFIGS[model_name]
        
        # Load tokenizer and model
        if HF_TOKEN:
            tokenizer = config["tokenizer_class"].from_pretrained(model_name, use_auth_token=HF_TOKEN)
            model = config["model_class"].from_pretrained(model_name, use_auth_token=HF_TOKEN)
        else:
            tokenizer = config["tokenizer_class"].from_pretrained(model_name)
            model = config["model_class"].from_pretrained(model_name)

        # Set pad token for causal models if missing
        if config["type"] == "causal" and tokenizer.pad_token is None:
            tokenizer.pad_token = tokenizer.eos_token

        loaded_model_name = model_name
        return model, tokenizer
        
    except Exception as e:
        raise RuntimeError(f"Failed to load model {model_name}: {str(e)}")

def authenticate_api_key(credentials: Optional[HTTPAuthorizationCredentials] = Depends(security)):
    if API_KEY:
        if not credentials or credentials.credentials != API_KEY:
            raise HTTPException(status_code=401, detail="Invalid or missing API key")
    return True

def generate_text_core(prompt, model_name, max_length, temperature, top_p, top_k):
    """Core text generation function"""
    try:
        config = MODEL_CONFIGS[model_name]
        model, tokenizer = load_model_and_tokenizer(model_name)

        if config["type"] == "causal":
            inputs = tokenizer.encode(prompt, return_tensors="pt", max_length=512, truncation=True)
            with torch.no_grad():
                outputs = model.generate(
                    inputs,
                    max_length=min(max_length + inputs.shape[1], 512),
                    temperature=temperature,
                    top_p=top_p,
                    top_k=top_k,
                    do_sample=True,
                    pad_token_id=tokenizer.pad_token_id,
                    num_return_sequences=1
                )
            generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
            return generated_text[len(prompt):].strip()

        elif config["type"] == "seq2seq":
            task_prompt = f"Complete this text: {prompt}" if "flan-t5" in model_name.lower() else prompt
            inputs = tokenizer(task_prompt, return_tensors="pt", max_length=512, truncation=True)
            with torch.no_grad():
                outputs = model.generate(
                    **inputs,
                    max_length=max_length,
                    temperature=temperature,
                    top_p=top_p,
                    top_k=top_k,
                    do_sample=True,
                    num_return_sequences=1
                )
            generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
            return generated_text.strip()

    except Exception as e:
        raise RuntimeError(f"Error generating text: {str(e)}")

# Gradio interface function
def generate_text_gradio(prompt, model_name, max_length, temperature, top_p, top_k, api_key=""):
    if API_KEY and api_key != API_KEY:
        return "Error: Invalid API key"
    
    try:
        return generate_text_core(prompt, model_name, max_length, temperature, top_p, top_k)
    except Exception as e:
        return f"Error: {str(e)}"

# Create FastAPI app
app = FastAPI(title="Multi-Model Text Generation API", version="1.0.0")

# API Routes
@app.post("/generate", response_model=GenerateResponse)
async def generate_text_api(
    request: GenerateRequest,
    authenticated: bool = Depends(authenticate_api_key)
):
    try:
        generated_text = generate_text_core(
            request.prompt,
            request.model_name,
            request.max_length,
            request.temperature,
            request.top_p,
            request.top_k
        )
        return GenerateResponse(
            generated_text=generated_text,
            model_used=request.model_name
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/models")
async def list_models():
    return {
        "models": [
            {
                "name": name,
                "description": config["description"],
                "size": config["size"],
                "type": config["type"]
            }
            for name, config in MODEL_CONFIGS.items()
        ]
    }

@app.get("/health")
async def health_check():
    return {"status": "healthy", "loaded_model": loaded_model_name}

# Create Gradio interface
with gr.Blocks(title="Multi-Model Text Generation Server") as demo:
    gr.Markdown("# Multi-Model Text Generation Server")
    gr.Markdown("Choose a model from the dropdown, enter a text prompt, and generate text.")

    with gr.Row():
        with gr.Column():
            model_selector = gr.Dropdown(
                label="Model",
                choices=list(MODEL_CONFIGS.keys()),
                value="gpt2",
                interactive=True
            )
            
            # Show model info
            model_info = gr.Markdown("**Model Info:** Original GPT-2, good for creative writing (117M)")
            
            def update_model_info(model_name):
                config = MODEL_CONFIGS[model_name]
                return f"**Model Info:** {config['description']} ({config['size']})"
            
            model_selector.change(update_model_info, inputs=model_selector, outputs=model_info)
            
            prompt_input = gr.Textbox(
                label="Text Prompt",
                placeholder="Enter the text prompt here...",
                lines=4
            )
            max_length_slider = gr.Slider(
                10, 200, 100, 10,
                label="Max Generation Length"
            )
            temperature_slider = gr.Slider(
                0.1, 2.0, 0.7, 0.1,
                label="Temperature"
            )
            top_p_slider = gr.Slider(
                0.1, 1.0, 0.9, 0.05,
                label="Top-p (nucleus sampling)"
            )
            top_k_slider = gr.Slider(
                1, 100, 50, 1,
                label="Top-k sampling"
            )
            if API_KEY:
                api_key_input = gr.Textbox(
                    label="API Key",
                    type="password",
                    placeholder="Enter API Key"
                )
            else:
                api_key_input = gr.Textbox(value="", visible=False)

            generate_btn = gr.Button("Generate Text", variant="primary")

        with gr.Column():
            output_textbox = gr.Textbox(
                label="Generated Text",
                lines=10,
                placeholder="Generated text will appear here..."
            )

    generate_btn.click(
        fn=generate_text_gradio,
        inputs=[prompt_input, model_selector, max_length_slider, temperature_slider, top_p_slider, top_k_slider, api_key_input],
        outputs=output_textbox
    )

    gr.Examples(
        examples=[
            ["Once upon a time in a distant galaxy,"],
            ["The future of artificial intelligence is"],
            ["In the heart of the ancient forest,"],
            ["The detective walked into the room and noticed"],
        ],
        inputs=prompt_input
    )

    # API documentation
    with gr.Accordion("API Documentation", open=False):
        gr.Markdown("""
        ## REST API Endpoints
        
        ### POST /generate
        Generate text using the specified model.
        
        **Request Body:**
        ```json
        {
            "prompt": "Your text prompt here",
            "model_name": "gpt2",
            "max_length": 100,
            "temperature": 0.7,
            "top_p": 0.9,
            "top_k": 50
        }
        ```
        
        **Response:**
        ```json
        {
            "generated_text": "Generated text...",
            "model_used": "gpt2",
            "status": "success"
        }
        ```
        
        ### GET /models
        List all available models.
        
        ### GET /health
        Check server health and loaded model status.
        
        **Example cURL:**
        ```bash
        curl -X POST "http://localhost:7860/generate" \
             -H "Content-Type: application/json" \
             -H "Authorization: Bearer YOUR_API_KEY" \
             -d '{"prompt": "Once upon a time", "model_name": "gpt2"}'
        ```
        """)

# Mount Gradio app to FastAPI
app = gr.mount_gradio_app(app, demo, path="/")

if __name__ == "__main__":
    auth_config = ("admin", ADMIN_PASSWORD) if ADMIN_PASSWORD else None
    
    # Launch with both FastAPI and Gradio
    demo.launch(
        auth=auth_config,
        server_name="0.0.0.0",
        server_port=7860,
        ssr_mode=False,
        share=False
    )