Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Request | |
| import requests | |
| import os | |
| import json | |
| app = FastAPI() | |
| GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY") | |
| GEMINI_ENDPOINT = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}" | |
| async def root(): | |
| return {"message": "AI Insight API is running. Use POST /api/ai-insight"} | |
| async def generate_insight(request: Request): | |
| body = await request.json() | |
| prompt_type = body.get("type", "general") | |
| data = body.get("data", []) | |
| base_prompt = "" | |
| if prompt_type == "compliance": | |
| base_prompt = f""" | |
| You are a compliance analyst. | |
| Analyze the following compliance document status data and return a JSON with: | |
| {{ | |
| "summary": "...", | |
| "risks": ["..."], | |
| "recommendations": ["..."] | |
| }} | |
| Data: | |
| {json.dumps(data, indent=2)} | |
| """ | |
| elif prompt_type == "resources": | |
| base_prompt = f""" | |
| You are a resource utilization expert. | |
| Analyze the resource usage data below. Return a JSON: | |
| {{ | |
| "summary": "...", | |
| "overutilized": ["..."], | |
| "underutilized": ["..."], | |
| "suggestions": ["..."] | |
| }} | |
| Data: | |
| {json.dumps(data, indent=2)} | |
| """ | |
| elif prompt_type == "participants": | |
| base_prompt = f""" | |
| You are a program analyst. | |
| Analyze participant growth metrics and return JSON like: | |
| {{ | |
| "summary": "...", | |
| "trends": ["..."], | |
| "risks": ["..."], | |
| "recommendations": ["..."] | |
| }} | |
| Data: | |
| {json.dumps(data, indent=2)} | |
| """ | |
| elif prompt_type == "intervention": | |
| base_prompt = f""" | |
| You are an incubation operations strategist. | |
| Analyze intervention frequency, completion and category data. Return a JSON: | |
| {{ | |
| "summary": "...", | |
| "effective_areas": ["..."], | |
| "gaps": ["..."], | |
| "recommendations": ["..."] | |
| }} | |
| Data: | |
| {json.dumps(data, indent=2)} | |
| """ | |
| elif prompt_type == "consultant": | |
| base_prompt = f""" | |
| You are evaluating consultant performance for an incubator program. | |
| Using metrics like hours, rating, and impact scores, return JSON like: | |
| {{ | |
| "summary": "...", | |
| "top_performers": ["..."], | |
| "underperformers": ["..."], | |
| "recommendations": ["..."] | |
| }} | |
| Data: | |
| {json.dumps(data, indent=2)} | |
| """ | |
| else: | |
| base_prompt = f""" | |
| Analyze the following data and return a useful JSON summary: | |
| {{ | |
| "summary": "...", | |
| "details": ["..."], | |
| "recommendations": ["..."] | |
| }} | |
| Data: | |
| {json.dumps(data, indent=2)} | |
| """ | |
| response = requests.post( | |
| GEMINI_ENDPOINT, | |
| headers={"Content-Type": "application/json"}, | |
| json={"contents": [{"parts": [{"text": base_prompt}]}]}, | |
| ) | |
| try: | |
| result = response.json() | |
| insight_text = result["candidates"][0]["content"]["parts"][0]["text"] | |
| return { | |
| "insight": insight_text | |
| } | |
| except Exception: | |
| return { | |
| "error": "Gemini response format invalid", | |
| "details": str(response.json()) | |
| } | |