File size: 1,498 Bytes
3cdb7c9
 
222fb60
 
 
3cdb7c9
3330fc9
3cdb7c9
 
3330fc9
3cdb7c9
 
 
3330fc9
3cdb7c9
 
 
3330fc9
3cdb7c9
 
 
 
3330fc9
3cdb7c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoProcessor
from PIL import Image
import requests
import torch

# Define the FastAPI app
app = FastAPI()

# Initialize model and processor at startup
processor = AutoProcessor.from_pretrained('allenai/Molmo-7B-D-0924', trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained('allenai/Molmo-7B-D-0924', trust_remote_code=True)

# Move the model to GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Request body structure
class GenerateRequest(BaseModel):
    image_url: str
    text_input: str

# API root endpoint
@app.get("/")
def root():
    return {"message": "Molmo-7B-D API is up and running!"}

# Text generation endpoint
@app.post("/generate/")
def generate_text(request: GenerateRequest):
    try:
        # Fetch image from URL
        response = requests.get(request.image_url, stream=True)
        image = Image.open(response.raw)

        # Preprocess inputs
        inputs = processor(images=[image], text=request.text_input, return_tensors="pt").to(device)

        # Generate text
        output_ids = model.generate(inputs["input_ids"], max_new_tokens=200)
        generated_text = processor.tokenizer.decode(output_ids[0], skip_special_tokens=True)

        return {"generated_text": generated_text}

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))