Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import requests
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import os
|
7 |
+
from fake_useragent import UserAgent
|
8 |
+
from fastapi.responses import RedirectResponse
|
9 |
+
|
10 |
+
# Load environment variables
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
# Get environment variables
|
14 |
+
API_URL = os.getenv("API_URL")
|
15 |
+
MODEL = os.getenv("MODEL")
|
16 |
+
Origin = os.getenv("ORIGIN")
|
17 |
+
|
18 |
+
# Initialize FastAPI app with custom documentation URLs
|
19 |
+
app = FastAPI(
|
20 |
+
title="Roast API",
|
21 |
+
description="API for generating custom roasts. For more details, visit [Roast API Documentation](https://roastapi-docs.netlify.app/).",
|
22 |
+
version="1.0.0",
|
23 |
+
docs_url="/docs",
|
24 |
+
openapi_url="/openapi.json"
|
25 |
+
)
|
26 |
+
|
27 |
+
class RoastRequest(BaseModel):
|
28 |
+
content: str
|
29 |
+
|
30 |
+
def fetch_roasts(content: str):
|
31 |
+
ua = UserAgent()
|
32 |
+
headers = {
|
33 |
+
'origin': Origin,
|
34 |
+
'user-agent': ua.random,
|
35 |
+
'accept': '*/*',
|
36 |
+
'accept-encoding': 'gzip, deflate, br, zstd',
|
37 |
+
'accept-language': 'en-US,en;q=0.9,en-IN;q=0.8',
|
38 |
+
}
|
39 |
+
|
40 |
+
try:
|
41 |
+
# Make API request with a 30-second timeout
|
42 |
+
response = requests.post(
|
43 |
+
API_URL,
|
44 |
+
headers=headers,
|
45 |
+
data={'content': content, 'model': MODEL},
|
46 |
+
timeout=30 # Add timeout here
|
47 |
+
)
|
48 |
+
response.raise_for_status() # Raises HTTPError for bad responses
|
49 |
+
|
50 |
+
# Parse the result and extract roasts
|
51 |
+
result = response.json().get('result', '')
|
52 |
+
roasts = re.findall(r'<roast>(.*?)</roast>', result)
|
53 |
+
return roasts
|
54 |
+
|
55 |
+
except requests.exceptions.Timeout:
|
56 |
+
raise HTTPException(status_code=504, detail="The request timed out.")
|
57 |
+
except requests.exceptions.RequestException as e:
|
58 |
+
raise HTTPException(status_code=503, detail=f"Network error: {e}")
|
59 |
+
except KeyError as e:
|
60 |
+
raise HTTPException(status_code=500, detail=f"Response format error: {e}")
|
61 |
+
except Exception as e:
|
62 |
+
raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
|
63 |
+
|
64 |
+
@app.get("/", include_in_schema=False)
|
65 |
+
async def root():
|
66 |
+
return RedirectResponse(url="https://roastapi-docs.netlify.app/")
|
67 |
+
|
68 |
+
@app.post("/generate-roasts/")
|
69 |
+
async def generate_roasts(request: RoastRequest):
|
70 |
+
roasts = fetch_roasts(request.content)
|
71 |
+
if not roasts:
|
72 |
+
raise HTTPException(status_code=404, detail="No roasts found.")
|
73 |
+
return {"roasts": roasts}
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
import uvicorn
|
77 |
+
print("Starting FastAPI server...")
|
78 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|