Spaces:
Runtime error
Runtime error
Delete main.py
Browse files
main.py
DELETED
|
@@ -1,129 +0,0 @@
|
|
| 1 |
-
from langgraph.graph import END
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
-
from fastapi.responses import JSONResponse
|
| 4 |
-
from pydantic import BaseModel
|
| 5 |
-
from typing import Dict, List, Any
|
| 6 |
-
from langgraph.graph import StateGraph
|
| 7 |
-
from service import research_task, seo_optimization_task, content_writing_task, refine_content, evaluate_content_quality, feedback_improvement, meeting_insights, upload_file
|
| 8 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
-
app = FastAPI()
|
| 10 |
-
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
|
| 11 |
-
class ContentState(Dict):
|
| 12 |
-
idea: str
|
| 13 |
-
company_name: str
|
| 14 |
-
services: Dict[str, List[str]] # Main services with their sub-services
|
| 15 |
-
service_area: Dict[str, Dict[str, str]] # Each area has multiple sub-service pages
|
| 16 |
-
research_data: str
|
| 17 |
-
seo_optimization: str
|
| 18 |
-
home_page: str
|
| 19 |
-
about_us_page: str
|
| 20 |
-
service_page: str
|
| 21 |
-
individual_service_page: Dict[str, str] # Single service pages
|
| 22 |
-
service_area_page: Dict[str, Dict[str, str]] # Each area with its sub-services
|
| 23 |
-
quality_score: int
|
| 24 |
-
feedback: str
|
| 25 |
-
content: str
|
| 26 |
-
data: str
|
| 27 |
-
text:str
|
| 28 |
-
meeting_point:str
|
| 29 |
-
file_path:str
|
| 30 |
-
workflow = StateGraph(ContentState)
|
| 31 |
-
|
| 32 |
-
# ✅ Define Workflow Steps
|
| 33 |
-
workflow.add_node("research_step", research_task)
|
| 34 |
-
workflow.add_node("seo_step", seo_optimization_task)
|
| 35 |
-
workflow.add_node("writing_step", content_writing_task)
|
| 36 |
-
workflow.add_node("refine_content", refine_content)
|
| 37 |
-
workflow.add_node("evaluate_content_quality", evaluate_content_quality)
|
| 38 |
-
workflow.add_node("feedback_improvement", feedback_improvement) # Node for quality rework
|
| 39 |
-
workflow.add_node("human_review", lambda state: state) # Human-in-the-loop review
|
| 40 |
-
workflow.add_node("meeting_insights",meeting_insights)
|
| 41 |
-
workflow.add_node("upload_file",upload_file)
|
| 42 |
-
# ✅ Define Transitions
|
| 43 |
-
workflow.set_entry_point("research_step")
|
| 44 |
-
workflow.set_entry_point("upload_file")
|
| 45 |
-
workflow.add_edge("upload_file", "meeting_insights")
|
| 46 |
-
workflow.add_edge("research_step", "seo_step")
|
| 47 |
-
workflow.add_edge("seo_step", "writing_step")
|
| 48 |
-
workflow.add_edge("meeting_insights", "writing_step")
|
| 49 |
-
workflow.add_edge("writing_step", "refine_content")
|
| 50 |
-
workflow.add_edge("refine_content", "evaluate_content_quality")
|
| 51 |
-
|
| 52 |
-
# Conditional Flow for Quality Check & Human Review
|
| 53 |
-
workflow.add_conditional_edges(
|
| 54 |
-
"evaluate_content_quality",
|
| 55 |
-
lambda state: "feedback_improvement" if state["quality_score"] <= 8 else "human_review",
|
| 56 |
-
{
|
| 57 |
-
"feedback_improvement": "feedback_improvement",
|
| 58 |
-
"human_review": "human_review"
|
| 59 |
-
}
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# ✅ Add Loopback from feedback_improvement to refine_content
|
| 63 |
-
workflow.add_edge("feedback_improvement", "evaluate_content_quality")
|
| 64 |
-
|
| 65 |
-
# ✅ Add Human-in-the-loop approval before finalization
|
| 66 |
-
workflow.add_edge("human_review", END)
|
| 67 |
-
|
| 68 |
-
# ✅ Compile the Graph
|
| 69 |
-
content_graph = workflow.compile()
|
| 70 |
-
class RequestModel(BaseModel):
|
| 71 |
-
idea: str
|
| 72 |
-
company_name: str
|
| 73 |
-
services: Dict[str, List[str]]
|
| 74 |
-
service_area: List[str]
|
| 75 |
-
class UpdateRequest(BaseModel):
|
| 76 |
-
page_key: List[str]
|
| 77 |
-
user_query: str
|
| 78 |
-
def generate_content(data): # Remove @app.post to make it an importable function
|
| 79 |
-
state = content_graph.invoke({
|
| 80 |
-
"idea": data["idea"],
|
| 81 |
-
"company_name": data["company_name"],
|
| 82 |
-
"services": data["services"],
|
| 83 |
-
"service_area": data["service_area"],
|
| 84 |
-
"quality_score": 0,
|
| 85 |
-
"file_path": data["file_path"]
|
| 86 |
-
})
|
| 87 |
-
|
| 88 |
-
response = {
|
| 89 |
-
"home_page": state.get("home_page", ""),
|
| 90 |
-
"about_us_page": state.get("about_us_page", ""),
|
| 91 |
-
"service_page": state.get("service_page", ""),
|
| 92 |
-
"individual_service_page": state.get("individual_service_page", {}),
|
| 93 |
-
"service_area_page": state.get("service_area_page", {})
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
return response # Return dictionary instead of JSONResponse
|
| 97 |
-
|
| 98 |
-
@app.put("/update-page/")
|
| 99 |
-
def update_page(state: dict, user_query: str):
|
| 100 |
-
"""
|
| 101 |
-
Updates the selected page content based on user feedback.
|
| 102 |
-
"""
|
| 103 |
-
current_content = state.get("page_content", "")
|
| 104 |
-
|
| 105 |
-
# Define the prompt for updating the content
|
| 106 |
-
prompt = f"""You are an expert content editor. Modify the content according to this exact request: {user_query}.
|
| 107 |
-
|
| 108 |
-
- If the request asks to remove specific text, completely delete it while keeping the content natural and professional.
|
| 109 |
-
- If the request involves replacing text, swap it exactly as instructed.
|
| 110 |
-
- If the request requires rewording, refine the text while keeping the meaning intact.
|
| 111 |
-
- Do not add explanations, comments, formatting hints, or additional modifications—only return the updated version of the content.
|
| 112 |
-
- Ensure that only the requested changes are made. Do not include the previous version or additional variations.
|
| 113 |
-
|
| 114 |
-
Here is the content before modification:
|
| 115 |
-
---
|
| 116 |
-
{current_content}
|
| 117 |
-
---
|
| 118 |
-
|
| 119 |
-
Return only the fully updated content without any extra details.
|
| 120 |
-
and update only the content that needs to be changed and shown with all content.
|
| 121 |
-
"""
|
| 122 |
-
|
| 123 |
-
# Call Gemini to process the update
|
| 124 |
-
updated_content = llm.invoke(prompt).content.strip()
|
| 125 |
-
|
| 126 |
-
# Ensure the modified content is updated correctly
|
| 127 |
-
return {"page_content": updated_content}
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|