Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -109,7 +109,7 @@ def signup():
|
|
| 109 |
user_ref = db.reference(f'users/{user.uid}')
|
| 110 |
user_data = {
|
| 111 |
'email': email,
|
| 112 |
-
'credits':
|
| 113 |
'is_admin': False,
|
| 114 |
'created_at': datetime.utcnow().isoformat()
|
| 115 |
}
|
|
@@ -155,9 +155,182 @@ def get_user_profile():
|
|
| 155 |
except Exception as e:
|
| 156 |
print(f"Error fetching user profile: {str(e)}")
|
| 157 |
return jsonify({'error': str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
# ---------- Video Generation Endpoint ----------
|
| 159 |
|
| 160 |
@app.route('/api/video/generate', methods=['POST'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
def generate_video():
|
| 162 |
try:
|
| 163 |
auth_header = request.headers.get('Authorization', '')
|
|
|
|
| 109 |
user_ref = db.reference(f'users/{user.uid}')
|
| 110 |
user_data = {
|
| 111 |
'email': email,
|
| 112 |
+
'credits': 30,
|
| 113 |
'is_admin': False,
|
| 114 |
'created_at': datetime.utcnow().isoformat()
|
| 115 |
}
|
|
|
|
| 155 |
except Exception as e:
|
| 156 |
print(f"Error fetching user profile: {str(e)}")
|
| 157 |
return jsonify({'error': str(e)}), 500
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
# -----------------------
|
| 162 |
+
# Content
|
| 163 |
+
# -----------------------
|
| 164 |
+
|
| 165 |
# ---------- Video Generation Endpoint ----------
|
| 166 |
|
| 167 |
@app.route('/api/video/generate', methods=['POST'])
|
| 168 |
+
def generate_video():
|
| 169 |
+
try:
|
| 170 |
+
auth_header = request.headers.get('Authorization', '')
|
| 171 |
+
if not auth_header.startswith('Bearer '):
|
| 172 |
+
return jsonify({'error': 'Authorization header missing or malformed'}), 401
|
| 173 |
+
token = auth_header.split(' ')[1]
|
| 174 |
+
uid = verify_token(token)
|
| 175 |
+
if not uid:
|
| 176 |
+
return jsonify({'error': 'Invalid token'}), 401
|
| 177 |
+
|
| 178 |
+
# Get user data and check credits
|
| 179 |
+
user_ref = db.reference(f'users/{uid}')
|
| 180 |
+
user_data = user_ref.get()
|
| 181 |
+
if not user_data or user_data.get('credits', 0) < 1:
|
| 182 |
+
return jsonify({'error': 'Insufficient credits'}), 400
|
| 183 |
+
|
| 184 |
+
# Deduct one credit
|
| 185 |
+
new_credits = user_data.get('credits', 0) - 5
|
| 186 |
+
user_ref.update({'credits': new_credits})
|
| 187 |
+
|
| 188 |
+
# Get prompt and generate video (simulation using Gemini)
|
| 189 |
+
req_data = request.get_json()
|
| 190 |
+
prompt = req_data.get('prompt', '')
|
| 191 |
+
if not prompt:
|
| 192 |
+
return jsonify({'error': 'Prompt is required'}), 400
|
| 193 |
+
|
| 194 |
+
generated_video = ""
|
| 195 |
+
|
| 196 |
+
return jsonify({
|
| 197 |
+
'success': True,
|
| 198 |
+
'video': generated_video,
|
| 199 |
+
'remaining_credits': new_credits
|
| 200 |
+
})
|
| 201 |
+
except Exception as e:
|
| 202 |
+
return jsonify({'error': str(e)}), 500
|
| 203 |
+
|
| 204 |
+
# ---------- Story Generation Endpoint ----------
|
| 205 |
+
|
| 206 |
+
@app.route('/api/story/generate', methods=['POST'])
|
| 207 |
+
def generate_story():
|
| 208 |
+
try:
|
| 209 |
+
auth_header = request.headers.get('Authorization', '')
|
| 210 |
+
if not auth_header.startswith('Bearer '):
|
| 211 |
+
return jsonify({'error': 'Authorization header missing or malformed'}), 401
|
| 212 |
+
token = auth_header.split(' ')[1]
|
| 213 |
+
uid = verify_token(token)
|
| 214 |
+
if not uid:
|
| 215 |
+
return jsonify({'error': 'Invalid token'}), 401
|
| 216 |
+
|
| 217 |
+
# Get user data and check credits
|
| 218 |
+
user_ref = db.reference(f'users/{uid}')
|
| 219 |
+
user_data = user_ref.get()
|
| 220 |
+
if not user_data or user_data.get('credits', 0) < 1:
|
| 221 |
+
return jsonify({'error': 'Insufficient credits'}), 400
|
| 222 |
+
|
| 223 |
+
# Deduct one credit
|
| 224 |
+
new_credits = user_data.get('credits', 0) - 5
|
| 225 |
+
user_ref.update({'credits': new_credits})
|
| 226 |
+
|
| 227 |
+
# Get prompt and generate video (simulation using Gemini)
|
| 228 |
+
req_data = request.get_json()
|
| 229 |
+
prompt = req_data.get('prompt', '')
|
| 230 |
+
if not prompt:
|
| 231 |
+
return jsonify({'error': 'Prompt is required'}), 400
|
| 232 |
+
|
| 233 |
+
model = configure_gemini()
|
| 234 |
+
# Call to Gemini to generate video content (this is a simulation)
|
| 235 |
+
response = "function(s) that does something"
|
| 236 |
+
# For demonstration, we assume the response contains a 'result' field
|
| 237 |
+
generated_story = response.result if hasattr(response, 'result') else "story generated based on parameters."
|
| 238 |
+
|
| 239 |
+
return jsonify({
|
| 240 |
+
'success': True,
|
| 241 |
+
'story': generated_story,
|
| 242 |
+
'remaining_credits': new_credits
|
| 243 |
+
})
|
| 244 |
+
except Exception as e:
|
| 245 |
+
return jsonify({'error': str(e)}), 500
|
| 246 |
+
|
| 247 |
+
# ---------- Audio Generation Endpoint ----------
|
| 248 |
+
@app.route('/api/audio/generate', methods=['POST'])
|
| 249 |
+
def generate_video():
|
| 250 |
+
try:
|
| 251 |
+
auth_header = request.headers.get('Authorization', '')
|
| 252 |
+
if not auth_header.startswith('Bearer '):
|
| 253 |
+
return jsonify({'error': 'Authorization header missing or malformed'}), 401
|
| 254 |
+
token = auth_header.split(' ')[1]
|
| 255 |
+
uid = verify_token(token)
|
| 256 |
+
if not uid:
|
| 257 |
+
return jsonify({'error': 'Invalid token'}), 401
|
| 258 |
+
|
| 259 |
+
# Get user data and check credits
|
| 260 |
+
user_ref = db.reference(f'users/{uid}')
|
| 261 |
+
user_data = user_ref.get()
|
| 262 |
+
if not user_data or user_data.get('credits', 0) < 1:
|
| 263 |
+
return jsonify({'error': 'Insufficient credits'}), 400
|
| 264 |
+
|
| 265 |
+
# Deduct one credit
|
| 266 |
+
new_credits = user_data.get('credits', 0) - 1
|
| 267 |
+
user_ref.update({'credits': new_credits})
|
| 268 |
+
|
| 269 |
+
# Get prompt and generate video (simulation using Gemini)
|
| 270 |
+
req_data = request.get_json()
|
| 271 |
+
prompt = req_data.get('prompt', '')
|
| 272 |
+
if not prompt:
|
| 273 |
+
return jsonify({'error': 'Prompt is required'}), 400
|
| 274 |
+
|
| 275 |
+
model = configure_gemini()
|
| 276 |
+
# Call to Gemini to generate video content (this is a simulation)
|
| 277 |
+
response = model.generate(prompt=prompt)
|
| 278 |
+
# For demonstration, we assume the response contains a 'result' field
|
| 279 |
+
generated_video = response.result if hasattr(response, 'result') else "Video content generated based on prompt."
|
| 280 |
+
|
| 281 |
+
return jsonify({
|
| 282 |
+
'success': True,
|
| 283 |
+
'video': generated_video,
|
| 284 |
+
'remaining_credits': new_credits
|
| 285 |
+
})
|
| 286 |
+
except Exception as e:
|
| 287 |
+
return jsonify({'error': str(e)}), 500
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
#----------Image Generation Endpoint ----------
|
| 291 |
+
@app.route('/api/image/generate', methods=['POST'])
|
| 292 |
+
def generate_video():
|
| 293 |
+
try:
|
| 294 |
+
auth_header = request.headers.get('Authorization', '')
|
| 295 |
+
if not auth_header.startswith('Bearer '):
|
| 296 |
+
return jsonify({'error': 'Authorization header missing or malformed'}), 401
|
| 297 |
+
token = auth_header.split(' ')[1]
|
| 298 |
+
uid = verify_token(token)
|
| 299 |
+
if not uid:
|
| 300 |
+
return jsonify({'error': 'Invalid token'}), 401
|
| 301 |
+
|
| 302 |
+
# Get user data and check credits
|
| 303 |
+
user_ref = db.reference(f'users/{uid}')
|
| 304 |
+
user_data = user_ref.get()
|
| 305 |
+
if not user_data or user_data.get('credits', 0) < 1:
|
| 306 |
+
return jsonify({'error': 'Insufficient credits'}), 400
|
| 307 |
+
|
| 308 |
+
# Deduct one credit
|
| 309 |
+
new_credits = user_data.get('credits', 0) - 1
|
| 310 |
+
user_ref.update({'credits': new_credits})
|
| 311 |
+
|
| 312 |
+
# Get prompt and generate video (simulation using Gemini)
|
| 313 |
+
req_data = request.get_json()
|
| 314 |
+
prompt = req_data.get('prompt', '')
|
| 315 |
+
if not prompt:
|
| 316 |
+
return jsonify({'error': 'Prompt is required'}), 400
|
| 317 |
+
|
| 318 |
+
model = configure_gemini()
|
| 319 |
+
# Call to Gemini to generate video content (this is a simulation)
|
| 320 |
+
response = model.generate(prompt=prompt)
|
| 321 |
+
# For demonstration, we assume the response contains a 'result' field
|
| 322 |
+
generated_video = response.result if hasattr(response, 'result') else "Video content generated based on prompt."
|
| 323 |
+
|
| 324 |
+
return jsonify({
|
| 325 |
+
'success': True,
|
| 326 |
+
'video': generated_video,
|
| 327 |
+
'remaining_credits': new_credits
|
| 328 |
+
})
|
| 329 |
+
except Exception as e:
|
| 330 |
+
return jsonify({'error': str(e)}), 500
|
| 331 |
+
|
| 332 |
+
# ---------- Preview Generation Endpoint ----------
|
| 333 |
+
@app.route('/api/preview/generate', methods=['POST'])
|
| 334 |
def generate_video():
|
| 335 |
try:
|
| 336 |
auth_header = request.headers.get('Authorization', '')
|