Spaces:
Sleeping
Sleeping
CocoonAI
commited on
Commit
Β·
8beeaaf
1
Parent(s):
c734caf
update
Browse files- app.py +86 -48
- profile_writer.py +85 -31
app.py
CHANGED
|
@@ -34,9 +34,10 @@ app = FastAPI()
|
|
| 34 |
|
| 35 |
# === Vault Path Helper ===
|
| 36 |
def get_user_vault_path(user_id: str) -> str:
|
| 37 |
-
base_path = os.path.join(tempfile.gettempdir(), "vaults"
|
| 38 |
-
os.
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
# === Request Models ===
|
| 42 |
class AskRequest(BaseModel):
|
|
@@ -97,71 +98,108 @@ async def ask(req: AskRequest):
|
|
| 97 |
async def save_note(req: NoteRequest):
|
| 98 |
try:
|
| 99 |
path = get_user_vault_path(req.user_id)
|
| 100 |
-
|
| 101 |
-
with open(note_path, "w", encoding="utf-8") as f:
|
| 102 |
f.write(req.content)
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
-
|
|
|
|
| 111 |
except Exception as e:
|
| 112 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 113 |
-
|
| 114 |
-
@app.post("/
|
| 115 |
-
async def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
user_id: str = Form(...),
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
resource_type: str = Form(...)
|
| 120 |
):
|
| 121 |
try:
|
| 122 |
-
safe_title = title.replace(" ", "_").lower()
|
| 123 |
-
file_path = f"Resources_and_Skills/resources/{safe_title}.md"
|
| 124 |
-
content = f"# π {title}\n\nType: {resource_type}\n\nLink: {link}"
|
| 125 |
-
|
| 126 |
vault_path = get_user_vault_path(user_id)
|
| 127 |
full_path = os.path.join(vault_path, file_path)
|
| 128 |
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 129 |
|
| 130 |
with open(full_path, "w", encoding="utf-8") as f:
|
| 131 |
-
f.write(
|
| 132 |
|
| 133 |
supabase_client.table("vault_files").upsert({
|
| 134 |
"user_id": user_id,
|
| 135 |
"path": file_path,
|
| 136 |
-
"content":
|
| 137 |
}).execute()
|
| 138 |
|
| 139 |
-
return {"status": "
|
| 140 |
except Exception as e:
|
| 141 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 142 |
|
| 143 |
-
@app.post("/
|
| 144 |
-
async def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
try:
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
write_profile_to_obsidian(req.user_id, req.profile_data, supabase_client=supabase_client)
|
| 151 |
-
return {"status": "Profile saved & Obsidian updated."}
|
| 152 |
-
except Exception as e:
|
| 153 |
-
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 154 |
-
|
| 155 |
-
@app.get("/debug/list_user_files")
|
| 156 |
-
def list_user_files(user_id: str = Query(...)):
|
| 157 |
-
path = get_user_vault_path(user_id)
|
| 158 |
-
if not os.path.exists(path):
|
| 159 |
-
return {"error": f"User path {path} not found"}
|
| 160 |
-
|
| 161 |
-
files = []
|
| 162 |
-
for root, _, filenames in os.walk(path):
|
| 163 |
-
for name in filenames:
|
| 164 |
-
rel_path = os.path.relpath(os.path.join(root, name), path)
|
| 165 |
-
files.append(rel_path)
|
| 166 |
-
|
| 167 |
-
return {"user_id": user_id, "files": files, "message": f"{len(files)} files found."}
|
|
|
|
| 34 |
|
| 35 |
# === Vault Path Helper ===
|
| 36 |
def get_user_vault_path(user_id: str) -> str:
|
| 37 |
+
base_path = os.path.join(tempfile.gettempdir(), "vaults")
|
| 38 |
+
user_path = os.path.join(base_path, f"user_{user_id}")
|
| 39 |
+
os.makedirs(user_path, exist_ok=True)
|
| 40 |
+
return user_path
|
| 41 |
|
| 42 |
# === Request Models ===
|
| 43 |
class AskRequest(BaseModel):
|
|
|
|
| 98 |
async def save_note(req: NoteRequest):
|
| 99 |
try:
|
| 100 |
path = get_user_vault_path(req.user_id)
|
| 101 |
+
with open(os.path.join(path, f"{req.title}.md"), "w", encoding="utf-8") as f:
|
|
|
|
| 102 |
f.write(req.content)
|
| 103 |
+
return {"status": "Note saved."}
|
| 104 |
+
except Exception as e:
|
| 105 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 106 |
|
| 107 |
+
@app.post("/profile")
|
| 108 |
+
async def save_profile(req: ProfileRequest):
|
| 109 |
+
try:
|
| 110 |
+
path = get_user_vault_path(req.user_id)
|
| 111 |
+
with open(os.path.join(path, "user_profile.json"), "w", encoding="utf-8") as f:
|
| 112 |
+
json.dump(req.profile_data, f, indent=2)
|
| 113 |
|
| 114 |
+
write_profile_to_obsidian(req.user_id, req.profile_data)
|
| 115 |
+
return {"status": "Profile saved & Obsidian updated."}
|
| 116 |
except Exception as e:
|
| 117 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 118 |
+
|
| 119 |
+
@app.post("/obsidian")
|
| 120 |
+
async def upload_obsidian_file(user_id: str, file: UploadFile = File(...)):
|
| 121 |
+
try:
|
| 122 |
+
path = get_user_vault_path(user_id)
|
| 123 |
+
with open(os.path.join(path, file.filename), "wb") as f:
|
| 124 |
+
f.write(await file.read())
|
| 125 |
+
return {"status": "File uploaded."}
|
| 126 |
+
except Exception as e:
|
| 127 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 128 |
+
|
| 129 |
+
@app.post("/sync_from_obsidian")
|
| 130 |
+
async def sync_from_obsidian(user_id: str):
|
| 131 |
+
try:
|
| 132 |
+
path = get_user_vault_path(user_id)
|
| 133 |
+
profile_path = os.path.join(path, "Profile/user_profile.md")
|
| 134 |
+
|
| 135 |
+
if not os.path.exists(profile_path):
|
| 136 |
+
return JSONResponse(status_code=404, content={"error": "Profile not found."})
|
| 137 |
+
|
| 138 |
+
with open(profile_path, "r", encoding="utf-8") as f:
|
| 139 |
+
content = f.read()
|
| 140 |
+
|
| 141 |
+
return {"status": "Profile loaded.", "content": content}
|
| 142 |
+
except Exception as e:
|
| 143 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 144 |
+
|
| 145 |
+
@app.post("/script")
|
| 146 |
+
async def generate_script(req: GenerateRequest):
|
| 147 |
+
return await generate_with_role(req, "You are a creative screenwriter.")
|
| 148 |
+
|
| 149 |
+
@app.post("/concepts")
|
| 150 |
+
async def generate_concepts(req: GenerateRequest):
|
| 151 |
+
return await generate_with_role(req, "You are an innovation engine.")
|
| 152 |
+
|
| 153 |
+
@app.post("/ideas")
|
| 154 |
+
async def generate_ideas(req: GenerateRequest):
|
| 155 |
+
return await generate_with_role(req, "You are a content strategist.")
|
| 156 |
+
|
| 157 |
+
async def generate_with_role(req: GenerateRequest, role: str):
|
| 158 |
+
try:
|
| 159 |
+
response = openai.chat.completions.create(
|
| 160 |
+
model="gpt-4",
|
| 161 |
+
messages=[
|
| 162 |
+
{"role": "system", "content": role},
|
| 163 |
+
{"role": "user", "content": req.prompt}
|
| 164 |
+
],
|
| 165 |
+
temperature=0.9
|
| 166 |
+
)
|
| 167 |
+
return {"response": response.choices[0].message.content}
|
| 168 |
+
except Exception as e:
|
| 169 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 170 |
+
|
| 171 |
+
@app.post("/update_file")
|
| 172 |
+
async def update_file(
|
| 173 |
user_id: str = Form(...),
|
| 174 |
+
file_path: str = Form(...),
|
| 175 |
+
new_content: str = Form(...)
|
|
|
|
| 176 |
):
|
| 177 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
vault_path = get_user_vault_path(user_id)
|
| 179 |
full_path = os.path.join(vault_path, file_path)
|
| 180 |
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 181 |
|
| 182 |
with open(full_path, "w", encoding="utf-8") as f:
|
| 183 |
+
f.write(new_content.strip())
|
| 184 |
|
| 185 |
supabase_client.table("vault_files").upsert({
|
| 186 |
"user_id": user_id,
|
| 187 |
"path": file_path,
|
| 188 |
+
"content": new_content.strip()
|
| 189 |
}).execute()
|
| 190 |
|
| 191 |
+
return {"status": "File updated successfully", "file": file_path}
|
| 192 |
except Exception as e:
|
| 193 |
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 194 |
|
| 195 |
+
@app.post("/add_resource")
|
| 196 |
+
async def add_resource(
|
| 197 |
+
user_id: str = Form(...),
|
| 198 |
+
title: str = Form(...),
|
| 199 |
+
link: str = Form(...),
|
| 200 |
+
resource_type: str = Form(...)
|
| 201 |
+
):
|
| 202 |
try:
|
| 203 |
+
safe_title = title.replace(" ", "_").lower()
|
| 204 |
+
file_path = f"Resources_and_Skills/resources/{safe_title}.md"
|
| 205 |
+
content = f"#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
profile_writer.py
CHANGED
|
@@ -1,41 +1,95 @@
|
|
| 1 |
import os
|
| 2 |
-
from datetime import datetime
|
| 3 |
|
| 4 |
def write_file(user_path, relative_path, content):
|
| 5 |
full_path = os.path.join(user_path, relative_path)
|
| 6 |
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 7 |
with open(full_path, "w", encoding="utf-8") as f:
|
| 8 |
f.write(content.strip() + "\n")
|
| 9 |
-
return full_path
|
| 10 |
|
| 11 |
-
def write_profile_to_obsidian(user_id: str, data: dict,
|
| 12 |
-
base_path
|
|
|
|
| 13 |
os.makedirs(base_path, exist_ok=True)
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
|
| 3 |
def write_file(user_path, relative_path, content):
|
| 4 |
full_path = os.path.join(user_path, relative_path)
|
| 5 |
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 6 |
with open(full_path, "w", encoding="utf-8") as f:
|
| 7 |
f.write(content.strip() + "\n")
|
|
|
|
| 8 |
|
| 9 |
+
def write_profile_to_obsidian(user_id: str, data: dict, base_path=None):
|
| 10 |
+
if base_path is None:
|
| 11 |
+
base_path = os.path.join("/data", "vaults", f"user_{user_id}")
|
| 12 |
os.makedirs(base_path, exist_ok=True)
|
| 13 |
+
print(f"[WRITE] Creating Obsidian structure for user: {user_id} at {base_path}")
|
| 14 |
|
| 15 |
+
# === Profile Folder ===
|
| 16 |
+
write_file(base_path, "Profile/user_profile.md", f"""
|
| 17 |
+
# π€ User Profile
|
| 18 |
+
- Experience Level: {data.get("experienceLevel", "")}
|
| 19 |
+
- Main Goal: {data.get("contentGoal", "")}
|
| 20 |
+
- Location: {data.get("country", "")}, {data.get("city", "")}
|
| 21 |
+
""")
|
| 22 |
+
|
| 23 |
+
write_file(base_path, "Profile/business_profile.md", f"""
|
| 24 |
+
# π’ Business Profile
|
| 25 |
+
- Type: {data.get("businessType", "")}
|
| 26 |
+
- Description: {data.get("businessDescription", "")}
|
| 27 |
+
- Niche: {data.get("niche", "")}
|
| 28 |
+
""")
|
| 29 |
+
|
| 30 |
+
write_file(base_path, "Profile/creator_personality.md", "# β¨ Creator Personality\nTo be discovered...")
|
| 31 |
+
|
| 32 |
+
# === Content Strategy ===
|
| 33 |
+
write_file(base_path, "Content_Strategy/content_goals.md", f"""
|
| 34 |
+
# π― Content Goals
|
| 35 |
+
- Goal: {data.get("contentGoal", "")}
|
| 36 |
+
- Niche: {data.get("niche", "")}
|
| 37 |
+
""")
|
| 38 |
+
|
| 39 |
+
write_file(base_path, "Content_Strategy/platforms_and_audience.md", f"""
|
| 40 |
+
# π£ Platforms & Audience
|
| 41 |
+
- Platforms: {", ".join(data.get("platforms", []))}
|
| 42 |
+
- Target Generation: {data.get("targetGeneration", "")}
|
| 43 |
+
- Time Available: {data.get("timeAvailable", "")}
|
| 44 |
+
- Monetization Intent: {data.get("monetizationIntent", "")}
|
| 45 |
+
""")
|
| 46 |
+
|
| 47 |
+
write_file(base_path, "Content_Strategy/content_types_and_niche.md", f"""
|
| 48 |
+
# π¦ Content Types & Niche
|
| 49 |
+
- Types: {", ".join(data.get("contentTypes", []))}
|
| 50 |
+
- Challenges: {data.get("mainChallenges", "")}
|
| 51 |
+
""")
|
| 52 |
+
|
| 53 |
+
write_file(base_path, "Content_Strategy/social_accounts.md", "# π Social Accounts\nAdd your accounts here.")
|
| 54 |
+
|
| 55 |
+
# === Resources & Skills ===
|
| 56 |
+
write_file(base_path, "Resources_and_Skills/current_challenges.md", f"""
|
| 57 |
+
# β Current Challenges
|
| 58 |
+
{data.get("mainChallenges", "")}
|
| 59 |
+
""")
|
| 60 |
+
|
| 61 |
+
write_file(base_path, "Resources_and_Skills/available_resources.md", f"""
|
| 62 |
+
# π οΈ Available Resources
|
| 63 |
+
{data.get("resources", "")}
|
| 64 |
+
""")
|
| 65 |
+
|
| 66 |
+
write_file(base_path, "Resources_and_Skills/learning_preferences.md", "# π Learning Preferences\nTo define.")
|
| 67 |
+
|
| 68 |
+
write_file(base_path, "Resources_and_Skills/existing_skills.md", "# π‘ Existing Skills\nList them here.")
|
| 69 |
+
|
| 70 |
+
# === Goals & Metrics ===
|
| 71 |
+
write_file(base_path, "Goals_and_Metrics/impact_goals.md", "# π Impact Goals\nTo define.")
|
| 72 |
+
|
| 73 |
+
write_file(base_path, "Goals_and_Metrics/success_metrics.md", "# π Success Metrics\nTo define.")
|
| 74 |
+
|
| 75 |
+
write_file(base_path, "Goals_and_Metrics/monetization_strategy.md", "# π° Monetization Strategy\nTo define.")
|
| 76 |
+
|
| 77 |
+
# === AI Context ===
|
| 78 |
+
write_file(base_path, "AI_Context/onboarding_summary.md", f"""
|
| 79 |
+
# π€ AI Onboarding Summary
|
| 80 |
+
This file summarizes the user context.
|
| 81 |
+
|
| 82 |
+
## Overview
|
| 83 |
+
- Goal: {data.get("contentGoal", "")}
|
| 84 |
+
- Experience: {data.get("experienceLevel", "")}
|
| 85 |
+
- Niche: {data.get("niche", "")}
|
| 86 |
+
- Audience: {data.get("targetGeneration", "")}
|
| 87 |
+
- Platforms: {", ".join(data.get("platforms", []))}
|
| 88 |
+
|
| 89 |
+
## Intentions
|
| 90 |
+
- Monetization: {data.get("monetizationIntent", "")}
|
| 91 |
+
- Available Time: {data.get("timeAvailable", "")}
|
| 92 |
+
|
| 93 |
+
## Notes
|
| 94 |
+
Automatically generated. Use for AI context.
|
| 95 |
+
""")
|