-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
125 lines (103 loc) Β· 4.24 KB
/
main.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
import os
from dotenv import load_dotenv
from agents import AgentManager
from typing import List
# β
Load environment variables from .env
load_dotenv()
app = FastAPI()
# β
Enable CORS (Modify `allow_origins` for security)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Change this to specific frontend URL if needed
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# β
Initialize the agent manager
agent_manager = AgentManager()
# β
Request Models
class SummarizeRequest(BaseModel):
text: str
class WritePostRequest(BaseModel):
topic: str
outline: str = ""
class SanitizeDataRequest(BaseModel):
data: str
class RefinePostRequest(BaseModel):
draft: str
class ValidatePostRequest(BaseModel):
topic: str
article: str
class GenerateCommentRequest(BaseModel):
post_content: str
class SentimentAnalysisRequest(BaseModel):
text: str
# β
Health Check Route
@app.get("/")
def home():
return {"message": "LinkedIn Automation API is running!"}
# β
API Routes
@app.post("/summarize")
def summarize(request: SummarizeRequest):
try:
summary = agent_manager.get_agent("summarize").execute(request.text)
validation_agent = agent_manager.get_agent("summarize_validator")
validation = validation_agent.execute(request.text, summary) if validation_agent else "Validation agent not found"
return {"summary": summary, "validation": validation}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/write_post")
def write_post(request: WritePostRequest):
try:
post = agent_manager.get_agent("write_post").execute(request.topic, request.outline)
validation = agent_manager.get_agent("write_post_validator").execute(request.topic, post)
return {"post": post, "validation": validation}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/sanitize_data")
def sanitize_data(request: SanitizeDataRequest):
try:
sanitized_data = agent_manager.get_agent("sanitize_data").execute(request.data)
validation = agent_manager.get_agent("sanitize_data_validator").execute(request.data, sanitized_data)
return {"sanitized_data": sanitized_data, "validation": validation}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/refine_post")
def refine_post(request: RefinePostRequest):
try:
refined_post = agent_manager.get_agent("refiner").execute(request.draft)
return {"refined_post": refined_post}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/validate_post")
def validate_post(request: ValidatePostRequest):
try:
validation = agent_manager.get_agent("validator").execute(request.topic, request.article)
return {"validation": validation}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/generate_comments") # β
Fixed endpoint name
def generate_comments(request: GenerateCommentRequest):
try:
comments = agent_manager.get_agent("generate_comment").execute(request.post_content)
if not isinstance(comments, list): # β
Ensure response is a list
comments = [comments] # Convert single string to a list
return {"comments": comments}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating comments: {str(e)}")
@app.post("/sentiment_analysis")
def sentiment_analysis(request: SentimentAnalysisRequest):
try:
sentiment = agent_manager.get_agent("sentiment_analysis").execute(request.text)
return {"sentiment": sentiment}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) # β
Fixed missing closing parenthesis
# β
Start FastAPI with Uvicorn for Railway Deployment
if __name__ == "__main__":
port = int(os.getenv("PORT", 8000)) # Railway provides dynamic PORT
print(f"π Server starting on http://0.0.0.0:{port} ...")
uvicorn.run(app, host="0.0.0.0", port=port)