Spaces:
Runtime error
Runtime error
| import os | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from huggingface_hub import InferenceClient | |
| from dotenv import load_dotenv | |
| # Load environment variables from .env | |
| load_dotenv() | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| if not GROQ_API_KEY: | |
| raise RuntimeError("Missing GROQ_API_KEY in .env") | |
| # Initialize Hugging Face Inference client (with Groq provider) | |
| client = InferenceClient( | |
| provider="groq", | |
| api_key=GROQ_API_KEY | |
| ) | |
| # Initialize FastAPI app | |
| app = FastAPI( | |
| title="Organism Space Biology Summarizer", | |
| description="Summarizes abstracts focusing on gravity, radiation, and temperature effects on organism growth.", | |
| version="1.0" | |
| ) | |
| # Request model | |
| class SummarizeRequest(BaseModel): | |
| abstract: str | |
| # Response model | |
| class SummarizeResponse(BaseModel): | |
| summary: str | |
| # System instruction (bias model toward space-biology context) | |
| SYSTEM_PROMPT = """ | |
| You are a scientific summarizer specialized in space biology. | |
| Given an abstract or text, produce a concise 1–3 sentence summary | |
| focused only on the effects of gravity, radiation, and temperature | |
| on the growth and behavior of organisms. Ignore unrelated details. | |
| Write clearly and factually. | |
| """ | |
| # Summarization endpoint | |
| async def summarize_text(req: SummarizeRequest): | |
| try: | |
| response = client.text_generation( | |
| model="distilbart-cnn-12-6", # HuggingFace summarization model | |
| inputs=f"{SYSTEM_PROMPT}\n\nAbstract:\n{req.abstract}\n\nSummary:", | |
| max_new_tokens=150, | |
| temperature=0.2 | |
| ) | |
| summary_text = response.strip() | |
| return SummarizeResponse(summary=summary_text) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Root route for quick health check | |
| async def root(): | |
| return {"message": "🚀 Space Biology Summarizer API is running!"} | |