Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161from fastapi import FastAPI, HTTPException, Queryfrom pydantic import BaseModel, Field, conlistimport torchimport torch.nn as nnimport os # Import the 'os' modulefrom typing import List# --- Model Definition (same as before) ---class NameGenderClassifierCNN(nn.Module):def __init__(self, vocab_size, embedding_dim, num_filters=64, filter_sizes=[2, 3, 4], dropout=0.5):super(NameGenderClassifierCNN, self).__init__()self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx=0)self.convs = nn.ModuleList([nn.Conv1d(in_channels=embedding_dim, out_channels=num_filters, kernel_size=fs)for fs in filter_sizes])self.fc1 = nn.Linear(len(filter_sizes) * num_filters, 100)self.fc2 = nn.Linear(100, 1)self.dropout = nn.Dropout(dropout)self.sigmoid = nn.Sigmoid()def forward(self, x):x = self.embedding(x)x = x.transpose(1, 2)conv_outputs = []for conv in self.convs:conv_out = torch.relu(conv(x))pool_out = torch.max_pool1d(conv_out, conv_out.shape[2])conv_outputs.append(pool_out.squeeze(2))x = torch.cat(conv_outputs, dim=1)x = self.dropout(x)x = torch.relu(self.fc1(x))x = self.dropout(x)x = self.fc2(x)return self.sigmoid(x).squeeze()# --- Utility Function (same as before, but adapted) ---def tokenize_name(name, char_to_idx, max_length):"""Tokenizes and pads a name."""name = str(name).lower()tokens = [char_to_idx.get(char, char_to_idx.get(' ', 1)) for char in name]# Pad or truncateif len(tokens) < max_length:tokens = tokens + [char_to_idx['<PAD>']] * (max_length - len(tokens))else:tokens = tokens[:max_length]return tokens# --- FastAPI Setup ---app = FastAPI(title="Indian Name Gender Prediction API",description="Predicts the gender of Indian names using a CNN model.",version="1.0")# --- Model Loading (on startup) ---MODEL_PATH = "models/indian_name_gender_model.pt" # Correct path within the spacedef load_model():"""Loads the model, char_to_idx, and max_name_length."""try:device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')checkpoint = torch.load(MODEL_PATH, map_location=device)char_to_idx = checkpoint['char_to_idx']max_name_length = checkpoint['max_name_length']config = checkpoint['model_config']model = NameGenderClassifierCNN(vocab_size=config['vocab_size'],embedding_dim=config['embedding_dim'],num_filters=config['num_filters'],filter_sizes=config['filter_sizes'])model.load_state_dict(checkpoint['model_state_dict'])model.to(device)model.eval() # Set to evaluation modereturn model, char_to_idx, max_name_length, deviceexcept Exception as e:raise Exception(f"Error loading model: {e}")# Load model at startuptry:model, char_to_idx, max_name_length, device = load_model()except Exception as e:print(f"Failed to load model: {e}")raise # Re-raise the exception to halt startup# --- Pydantic Models (for request/response validation) ---class PredictionRequest(BaseModel):names: conlist(str, min_length=1) = Field(..., example=["Aarav", "Anika"])threshold: float = Field(0.5, ge=0.0, le=1.0, description="Probability threshold for classifying as male.")class PredictionResponse(BaseModel):predictions: List[dict] = Field(..., example=[{"name": "Aarav", "predicted_gender": "Male", "male_probability": 0.95, "confidence": 0.95},{"name": "Anika", "predicted_gender": "Female", "male_probability": 0.05, "confidence": 0.95}])# --- Prediction Function ---def predict_gender(name: str, model, char_to_idx, max_length, device, threshold: float = 0.5) -> tuple[str, float, float]:"""Predicts gender for a single name. Includes threshold."""tokenized_name = tokenize_name(name, char_to_idx, max_length)input_tensor = torch.tensor([tokenized_name], dtype=torch.long).to(device)with torch.no_grad():output = model(input_tensor)probability = output.item()predicted_gender = 'Male' if probability >= threshold else 'Female'confidence = probability if probability >= threshold else 1 - probabilityreturn predicted_gender, probability, confidence# --- API Endpoints ---@app.get("/", response_model=str)async def read_root():return "Welcome to the Indian Name Gender Prediction API. Use the /predict endpoint."@app.post("/predict", response_model=PredictionResponse)async def predict(request: PredictionRequest):"""Predicts the gender of one or more Indian names."""try:predictions = []for name in request.names:gender, prob, conf = predict_gender(name, model, char_to_idx, max_name_length, device, request.threshold)predictions.append({"name": name,"predicted_gender": gender,"male_probability": prob,"confidence": conf})return {"predictions": predictions}except Exception as e:raise HTTPException(status_code=500, detail=str(e))@app.get("/predict_single")async def predict_single(name: str = Query(..., description="The name to predict."),threshold: float = Query(0.5, ge=0.0, le=1.0, description="Probability threshold for classifying as male.")):"""Predicts gender for a *single* name, provided as a query parameter."""try:gender, prob, conf = predict_gender(name, model, char_to_idx, max_name_length, device, threshold)return {"name": name,"predicted_gender": gender,"male_probability": prob,"confidence": conf}except Exception as e:raise HTTPException(status_code=500, detail=str(e))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136import osfrom typing import Listfrom fastapi import FastAPI, HTTPException, statusfrom pydantic import BaseModelfrom google import genaifrom google.genai import typesfrom google.protobuf.json_format import MessageToDict # For converting to dictapp = FastAPI()# Load API key from environment variableGEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")if not GEMINI_API_KEY:raise ValueError("The GEMINI_API_KEY environment variable is not set.")genai.configure(api_key=GEMINI_API_KEY)client = genai.GenerativeModel('gemini-pro') # Use a consistent model. 'gemini-pro' is better for text.class TranslationRequest(BaseModel):text: strtarget_language: str # Accept full language name, e.g., "Telugu", "Tamil", "Hindi"source_language: str = None # Optional: User *might* provide the source.class TranslationResponse(BaseModel):translated_text: strsource_language: str # Always return the detected/used source languagetarget_language: str# --- Helper Functions ---def detect_language_and_options(text: str):"""Detects the language of the input text and provides translation options."""contents = [types.Content(role="user",parts=[types.Part.from_text(text=text)],),types.Content(role="model",parts=[types.Part.from_text(text="""Please identify the language of the text provided and then offer translation options as numbered choices (1-5). Use this format: "The text is in [Language]. Choose a language to translate to: 1. [Option 1], 2. [Option 2], 3. [Option 3], 4. [Option 4], 5. [Option 5]".""")])]response = client.generate_content(contents)# Extract language and make options consistent. Robust parsing.try:response_text = response.textsource_language = response_text.split("The text is in ")[1].split(".")[0].strip()options_str = response_text.split("Choose a language to translate to:")[1].strip()options_list = [opt.split(". ")[1].strip() for opt in options_str.split(", ")]# Ensure we have *exactly* 5 options, padding if needed.while len(options_list) < 5:options_list.append("Option Not Available") # Or some other placeholderoptions_list = options_list[:5]options = {str(i + 1): lang for i, lang in enumerate(options_list)}return source_language, optionsexcept (IndexError, AttributeError): # Handle parsing errors gracefullyraise HTTPException(status_code=500, detail="Error processing language detection response.")def translate_with_gemini(text: str, source_language: str, target_language: str) -> str:"""Translates text using Gemini Pro, handling language codes correctly."""# More direct prompting style. No few-shot examples needed for a simple translation task.prompt = f"Translate the following text from {source_language} to {target_language}:\n\n{text}"response = client.generate_content(prompt)try:return response.textexcept (AttributeError, IndexError) as e:raise HTTPException(status_code=500, detail=f"Error from Gemini API: {e}")@app.post("/translate", response_model=TranslationResponse, status_code=status.HTTP_200_OK)async def translate(request: TranslationRequest):"""Translates text from a source language to a target language."""if not request.text:raise HTTPException(status_code=400, detail="Text to translate cannot be empty.")if not request.target_language:raise HTTPException(status_code=400, detail="Target language must be provided.")if request.source_language: # User provided source language. Use it directly.source_language = request.source_languageelse: # Detect the languagetry:source_language, _ = detect_language_and_options(request.text) # We don't need options hereexcept HTTPException as e: # Re-raise HTTP exceptions from the helper function.raise eexcept Exception as e: # Catch any other unexpected errors.raise HTTPException(status_code=500, detail=f"Language detection failed: {e}")# Validate the target language against a reasonable set of supported languages.supported_languages = ["English", "Hindi", "Telugu", "Marathi", "Bengali", "Tamil", "Spanish", "French", "German", "Japanese", "Chinese"] # Add more as needed.if request.target_language not in supported_languages:raise HTTPException(status_code=400, detail=f"Target language '{request.target_language}' is not supported. Supported languages: {', '.join(supported_languages)}")try:translated_text = translate_with_gemini(request.text, source_language, request.target_language)return TranslationResponse(translated_text=translated_text, source_language=source_language, target_language=request.target_language)except HTTPException as e:raise eexcept Exception as e:raise HTTPException(status_code=500, detail=f"Translation failed: {e}")@app.post("/detect_language", status_code=status.HTTP_200_OK)async def detect_language(text: str = ""): # Simpler input, just the text"""Detects the language of the input text and provides translation options."""if not text:raise HTTPException(status_code=400, detail="Text to detect cannot be empty.")try:source_language, options = detect_language_and_options(text)return {"source_language": source_language, "translation_options": options}except HTTPException as e:raise eexcept Exception as e:raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
app.py+102-127