lawallanre's picture
Update app.py
58cf445 verified
from fastapi import FastAPI, File, Form, UploadFile, Request, HTTPException
from typing import Annotated, Optional
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from PIL import Image
import os
import google.generativeai as genai
import google.ai.generativelanguage as glm
from typing import Dict
import io
from fastapi.middleware.cors import CORSMiddleware
from imageFunct import get_image_data
# Initialize FastAPI app
app = FastAPI()
# Configure API key for Google Generative AI
os.environ['GOOGLE_API_KEY'] = "AIzaSyDAG_Xl66vh4ceY81UXe3vrdwP6wIAkpBs"
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
# Configure CORS
origins = [
"http://localhost:3000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def read_index():
return "This is the index page"
@app.post('/classify')
async def main(
image: UploadFile = File(...),
action_id: Optional[str] = Form(None)
):
try:
image_data = await image.read()
image = Image.open(io.BytesIO(image_data))
result = get_image_data(image)
return JSONResponse(content=result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating content: {e}")
# Run the application
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7980)