Spaces:
Configuration error
Configuration error
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from dotenv import load_dotenv | |
| import google.generativeai as genai | |
| import os | |
| # Load .env | |
| load_dotenv() | |
| API_KEY = os.getenv("GEMINI_API_KEY") | |
| if not API_KEY: | |
| raise RuntimeError("GEMINI_API_KEY not found in .env") | |
| # Gemini config | |
| genai.configure(api_key=API_KEY) | |
| SYSTEM_INSTRUCTION = """ | |
| You are a senior software engineer. | |
| Provide structured, technical, and accurate answers. | |
| Explain reasoning step by step when needed. | |
| Focus on best practices and real-world solutions. | |
| Avoid unnecessary explanations. | |
| """ | |
| model = genai.GenerativeModel( | |
| model_name="gemini-2.5-flash", | |
| system_instruction=SYSTEM_INSTRUCTION, | |
| ) | |
| app = FastAPI() | |
| # CORS | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| class TextRequest(BaseModel): | |
| text: str | |
| async def summarize(req: TextRequest): | |
| text = req.text.strip() | |
| if not text: | |
| raise HTTPException(status_code=400, detail="Text is required") | |
| try: | |
| response = model.generate_content(text) | |
| return {"summary": response.text.strip()} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("server:app", host="0.0.0.0", port=4000, reload=True) | |