File size: 2,197 Bytes
64c0b0e
 
 
 
e0e5738
64c0b0e
 
 
 
e0e5738
 
 
 
11ba705
 
64c0b0e
11ba705
 
 
 
 
 
 
 
64c0b0e
 
11ba705
 
 
 
 
 
64c0b0e
 
 
 
c36fb16
 
64c0b0e
 
 
 
 
 
 
 
e0e5738
4c64189
bf91a0e
64c0b0e
bf91a0e
64c0b0e
 
 
 
4c64189
 
64c0b0e
 
 
e0e5738
64c0b0e
 
 
 
 
e71208c
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch
import logging

# Initialize FastAPI app
app = FastAPI()

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Load the Google Gemma 7B model and tokenizer
model_id = "google/gemma-7b"  # Use Google Gemma 7B
tokenizer = AutoTokenizer.from_pretrained(model_id)

# Load the model with 4-bit quantization to reduce VRAM usage
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float16,  # Use half-precision for faster inference
    device_map="auto",          # Automatically offload to available GPUs
    load_in_4bit=True           # Enable 4-bit quantization
)

# Create a text generation pipeline
pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    device="cuda" if torch.cuda.is_available() else "cpu"
)

# Define request body schema
class TextGenerationRequest(BaseModel):
    prompt: str
    max_new_tokens: int = 50  # Reduce this for faster responses
    temperature: float = 0.7  # Lower for more deterministic outputs
    top_k: int = 50
    top_p: float = 0.9
    do_sample: bool = True

# Define API endpoint
@app.post("/generate-text")
async def generate_text(request: TextGenerationRequest):
    try:
        logger.info("Generating text...")
        
        # Generate text using the pipeline with the user's prompt
        outputs = pipe(
            request.prompt,  # Use the user's prompt directly
            max_new_tokens=request.max_new_tokens,
            temperature=request.temperature,
            top_k=request.top_k,
            top_p=request.top_p,
            do_sample=request.do_sample,
            return_full_text=False  # Exclude the input prompt from the output
        )
        return {"generated_text": outputs[0]["generated_text"]}
    except Exception as e:
        logger.error(f"Error generating text: {e}")
        raise HTTPException(status_code=500, detail=str(e))

# Add a root endpoint for health checks
@app.get("/test")
async def root():
    return {"message": "API is running!"}