CocoonAI commited on
Commit
f1fd7f6
·
1 Parent(s): ef116ed
Files changed (2) hide show
  1. app.py +34 -20
  2. profile_writer.py +3 -5
app.py CHANGED
@@ -3,7 +3,7 @@ import os
3
  import json
4
  import tempfile
5
  import openai
6
- from fastapi import FastAPI, UploadFile, File, Query
7
  from fastapi.responses import JSONResponse
8
  from pydantic import BaseModel
9
  from sentence_transformers import SentenceTransformer
@@ -69,7 +69,6 @@ def ping():
69
  async def test_connection():
70
  return {"status": "ok", "message": "Connected successfully"}
71
 
72
- # === Ask endpoint ===
73
  @app.post("/ask")
74
  async def ask(req: AskRequest):
75
  try:
@@ -95,7 +94,6 @@ async def ask(req: AskRequest):
95
  except Exception as e:
96
  return JSONResponse(status_code=500, content={"error": str(e)})
97
 
98
- # === Save note ===
99
  @app.post("/note")
100
  async def save_note(req: NoteRequest):
101
  try:
@@ -106,7 +104,6 @@ async def save_note(req: NoteRequest):
106
  except Exception as e:
107
  return JSONResponse(status_code=500, content={"error": str(e)})
108
 
109
- # === Save profile ===
110
  @app.post("/profile")
111
  async def save_profile(req: ProfileRequest):
112
  try:
@@ -115,12 +112,10 @@ async def save_profile(req: ProfileRequest):
115
  json.dump(req.profile_data, f, indent=2)
116
 
117
  write_profile_to_obsidian(req.user_id, req.profile_data)
118
-
119
  return {"status": "Profile saved & Obsidian updated."}
120
  except Exception as e:
121
  return JSONResponse(status_code=500, content={"error": str(e)})
122
 
123
- # === Upload file ===
124
  @app.post("/obsidian")
125
  async def upload_obsidian_file(user_id: str, file: UploadFile = File(...)):
126
  try:
@@ -131,7 +126,6 @@ async def upload_obsidian_file(user_id: str, file: UploadFile = File(...)):
131
  except Exception as e:
132
  return JSONResponse(status_code=500, content={"error": str(e)})
133
 
134
- # === Sync from file ===
135
  @app.post("/sync_from_obsidian")
136
  async def sync_from_obsidian(user_id: str):
137
  try:
@@ -148,7 +142,6 @@ async def sync_from_obsidian(user_id: str):
148
  except Exception as e:
149
  return JSONResponse(status_code=500, content={"error": str(e)})
150
 
151
- # === Generate with role ===
152
  @app.post("/script")
153
  async def generate_script(req: GenerateRequest):
154
  return await generate_with_role(req, "You are a creative screenwriter.")
@@ -175,17 +168,38 @@ async def generate_with_role(req: GenerateRequest, role: str):
175
  except Exception as e:
176
  return JSONResponse(status_code=500, content={"error": str(e)})
177
 
178
- # === Debug route ===
179
- @app.get("/debug/list_user_files")
180
- def list_user_files(user_id: str = Query(...)):
181
- path = os.path.join(tempfile.gettempdir(), "vaults", f"user_{user_id}")
182
- if not os.path.exists(path):
183
- return {"error": f"User path {path} not found"}
 
 
 
 
 
 
 
184
 
185
- files = []
186
- for root, _, filenames in os.walk(path):
187
- for name in filenames:
188
- rel_path = os.path.relpath(os.path.join(root, name), path)
189
- files.append(rel_path)
190
 
191
- return {"user_id": user_id, "files": files, "message": f"{len(files)} files found."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import json
4
  import tempfile
5
  import openai
6
+ from fastapi import FastAPI, UploadFile, File, Query, Form
7
  from fastapi.responses import JSONResponse
8
  from pydantic import BaseModel
9
  from sentence_transformers import SentenceTransformer
 
69
  async def test_connection():
70
  return {"status": "ok", "message": "Connected successfully"}
71
 
 
72
  @app.post("/ask")
73
  async def ask(req: AskRequest):
74
  try:
 
94
  except Exception as e:
95
  return JSONResponse(status_code=500, content={"error": str(e)})
96
 
 
97
  @app.post("/note")
98
  async def save_note(req: NoteRequest):
99
  try:
 
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:
 
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:
 
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:
 
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.")
 
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,5 +1,4 @@
1
  import os
2
- import tempfile
3
 
4
  def write_file(user_path, relative_path, content):
5
  full_path = os.path.join(user_path, relative_path)
@@ -7,10 +6,9 @@ def write_file(user_path, relative_path, content):
7
  with open(full_path, "w", encoding="utf-8") as f:
8
  f.write(content.strip() + "\n")
9
 
10
- def write_profile_to_obsidian(user_id: str, data: dict):
11
- # Utilise un chemin temporaire valide pour Hugging Face Spaces
12
- base_temp_dir = tempfile.gettempdir()
13
- base_path = os.path.join(base_temp_dir, "vaults", f"user_{user_id}")
14
  os.makedirs(base_path, exist_ok=True)
15
  print(f"[WRITE] Creating Obsidian structure for user: {user_id} at {base_path}")
16
 
 
1
  import os
 
2
 
3
  def write_file(user_path, relative_path, content):
4
  full_path = os.path.join(user_path, relative_path)
 
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