import os import tempfile from fastapi import FastAPI, File, UploadFile, Form from fastapi.responses import JSONResponse from gradio_client import Client, handle_file app = FastAPI() HF_TOKEN = os.getenv("HF_TOKEN") if not HF_TOKEN: raise ValueError("HF_TOKEN environment variable is not set.") try: client = Client("Luisgust/moondream1", hf_token=HF_TOKEN) except Exception as e: print(f"Failed to initialize Gradio client: {e}") raise @app.post("/get_caption") async def get_caption(image: UploadFile = File(...), context: str = Form(...)): try: # Create a temporary file with tempfile.NamedTemporaryFile(delete=False) as temp_file: # Write the uploaded file contents to the temp file contents = await image.read() temp_file.write(contents) temp_file_path = temp_file.name # Use the temporary file path with handle_file or any other processing image_data = handle_file(temp_file_path) # Call the Gradio API result = client.predict( image=image_data, question=context, api_name="/answer_question" ) # Return the result as a JSON response return JSONResponse(content={"caption": result}) except Exception as e: print(f"Error during prediction: {e}") return JSONResponse(content={"error": str(e)}, status_code=500) finally: # Remove the temporary file if os.path.exists(temp_file_path): os.remove(temp_file_path) # Run the server with: uvicorn main:app --reload