Spaces:
Runtime error
Runtime error
from fastapi import FastAPI, HTTPException | |
from fastapi.responses import StreamingResponse | |
import qrcode | |
import io | |
app = FastAPI() | |
async def generate_qr(text: str): | |
if not text or not text.strip(): | |
raise HTTPException(status_code=400, detail="Text input is empty") | |
qr = qrcode.QRCode( | |
version=1, | |
error_correction=qrcode.constants.ERROR_CORRECT_H, | |
box_size=10, | |
border=4, | |
) | |
qr.add_data(text.strip()) | |
qr.make(fit=True) | |
img = qr.make_image(fill_color="#0057D8", back_color="white").convert("RGB") | |
buf = io.BytesIO() | |
img.save(buf, format="PNG") | |
buf.seek(0) | |
return StreamingResponse(buf, media_type="image/png") | |