Spaces:
Running
Running
File size: 2,562 Bytes
245c6b4 1f97cf6 9eec479 1f97cf6 9eec479 245c6b4 1f97cf6 b15e27e 1f97cf6 245c6b4 1f97cf6 245c6b4 1f97cf6 245c6b4 1f97cf6 245c6b4 1f97cf6 245c6b4 1f97cf6 245c6b4 |
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 |
from fastapi import FastAPI, File, UploadFile, Form
from transformers import AutoModel, AutoTokenizer
import uvicorn
from PIL import Image
import io
import torch
import os
from huggingface_hub import login
app = FastAPI(title="Image-Text API")
# Model ve tokenizer için global değişkenler
model = None
tokenizer = None
# HuggingFace token ile giriş yap
if "HUGGINGFACE_TOKEN" in os.environ:
login(token=os.environ["HUGGINGFACE_TOKEN"])
async def init_model():
global model, tokenizer
# Model ve tokenizer'ı yükle
model = AutoModel.from_pretrained(
'openbmb/MiniCPM-V-2_6',
trust_remote_code=True,
attn_implementation='eager',
torch_dtype=torch.bfloat16
)
model = model.eval()
if torch.cuda.is_available():
model = model.cuda()
tokenizer = AutoTokenizer.from_pretrained(
'openbmb/MiniCPM-V-2_6',
trust_remote_code=True
)
@app.on_event("startup")
async def startup_event():
await init_model()
@app.post("/process")
async def process_image_text(
image: UploadFile = File(...),
prompt: str = Form(...),
stream: bool = Form(False)
):
try:
# Resmi oku ve PIL Image'a dönüştür
image_content = await image.read()
pil_image = Image.open(io.BytesIO(image_content)).convert('RGB')
# Mesaj formatını hazırla
msgs = [{'role': 'user', 'content': [pil_image, prompt]}]
if stream:
# Streaming yanıt için generator
async def generate():
result = model.chat(
image=None,
msgs=msgs,
tokenizer=tokenizer,
sampling=True,
stream=True
)
for text in result:
yield {"text": text}
return generate()
else:
# Normal yanıt
result = model.chat(
image=None,
msgs=msgs,
tokenizer=tokenizer
)
return {
"status": "success",
"result": result
}
except Exception as e:
return {
"status": "error",
"message": str(e)
}
@app.get("/")
async def root():
return {
"message": "Image-Text API'ye hoş geldiniz",
"usage": "POST /process endpoint'ini kullanın"
}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000) |