File size: 3,529 Bytes
271a193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8255be5
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

# 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"}