description / app /main.py
parthgajera's picture
Update app/main.py
8255be5 verified
# from fastapi import FastAPI
# from pydantic import BaseModel
# from .paraphrasing import get_paraphrased_text
# import re
# # Initialize FastAPI app
# app = FastAPI()
# # Model to handle a single description input
# class DescriptionInput(BaseModel):
# description: str
# # class DescriptionInput(BaseModel):
# # description: str
# # tone: str = "fun and engaging"
# # Define the route to handle paraphrasing a single description
# @app.post("/rewrite_description/")
# async def rewrite_description_endpoint(input_data: DescriptionInput):
# # Escape special characters in the input description to ensure it's safe for processing
# sanitized_description = sanitize_input(input_data.description)
# # Get the paraphrased description
# rewritten = get_paraphrased_text(sanitized_description)
# # rewritten = get_paraphrased_text(sanitized_description, tone=input_data.tone)
# return {"rewritten": rewritten}
# def sanitize_input(description: str) -> str:
# """
# Sanitize the user input by escaping special characters and preserving necessary formatting.
# """
# # Escape special characters like " and ' to prevent issues in JSON or code
# description = re.sub(r'(["\\])', r'\\\1', description) # Escape quotes and backslashes
# # Optional: Handle apostrophes and possessive cases by ensuring they're preserved
# # No need for changes here unless you want to handle more complex scenarios
# description = re.sub(r"(\w)'(\w)", r"\1\\'\2", description) # Escape apostrophes
# # Ensure newlines (\n) are preserved for text formatting
# description = description.replace("\n", "\\n") # Escaped newlines for formatting preservation
# # Optional: Handle other special characters like colons, dashes, or brackets (if needed)
# # (This is typically not necessary unless specific cases require additional escape logic)
# return description
# from fastapi import FastAPI
# from pydantic import BaseModel
# from app.paraphrasing import get_paraphrased_text
# # Initialize FastAPI app
# app = FastAPI()
# # Model to handle a single description input
# class DescriptionInput(BaseModel):
# description: str
# # Define the route to handle paraphrasing a single description
# @app.post("/rewrite_description/")
# async def rewrite_description_endpoint(input_data: DescriptionInput):
# rewritten = await get_paraphrased_text(input_data.description)
# return {"rewritten": rewritten}
from fastapi import FastAPI, HTTPException, Header, Depends
from pydantic import BaseModel
from app.paraphrasing import get_paraphrased_text
import os
# Initialize FastAPI app
app = FastAPI()
# Model to handle a single description input
class DescriptionInput(BaseModel):
description: str
# Load the API Key from environment variables (Hugging Face secret)
API_KEY = os.getenv("API_KEY")
# Function to validate the API key
def validate_api_key(x_api_key: str = Header(...)) -> str:
if x_api_key != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API Key")
return x_api_key
# Define the route to handle paraphrasing a single description
@app.post("/rewrite_description/")
async def rewrite_description_endpoint(
input_data: DescriptionInput,
api_key: str = Depends(validate_api_key)
):
rewritten = await get_paraphrased_text(input_data.description)
return {"rewritten": rewritten}
@app.get("/")
def read_root():
return {"message": "Welcome to the Description Rewriter API"}