Spanicin commited on
Commit
c3866eb
·
verified ·
1 Parent(s): c19a9dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -5
app.py CHANGED
@@ -272,7 +272,7 @@ def translate_text(text_prompt, target_language):
272
  )
273
  return response
274
 
275
- def chat_avatar(text_prompt):
276
  response = client.chat.completions.create(
277
  model="gpt-4o-mini",
278
  messages=[{"role": "system", "content": "Answer using the minimum words you can ever use."},
@@ -284,6 +284,35 @@ def chat_avatar(text_prompt):
284
  )
285
  return response
286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
 
288
 
289
  @app.route("/run", methods=['POST'])
@@ -306,6 +335,7 @@ def generate_video():
306
  return jsonify({'error': 'Input text prompt cannot be blank'}), 400
307
 
308
  voice_cloning = request.form.get('voice_cloning', 'no')
 
309
  target_language = request.form.get('target_language', 'original_text')
310
  print('target_language',target_language)
311
  pose_style = int(request.form.get('pose_style', 1))
@@ -324,10 +354,30 @@ def generate_video():
324
  # # response = await translate_text_async(text_prompt, target_language)
325
  # text_prompt = response.choices[0].message.content.strip()
326
 
327
- response = chat_avatar(text_prompt)
328
- text_prompt = response.choices[0].message.content.strip()
329
- app.config['text_prompt'] = text_prompt
330
- print('Final text prompt: ',text_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
332
  source_image_path = save_uploaded_file(source_image, 'source_image.png',TEMP_DIR)
333
  print(source_image_path)
 
272
  )
273
  return response
274
 
275
+ def openai_chat_avatar(text_prompt):
276
  response = client.chat.completions.create(
277
  model="gpt-4o-mini",
278
  messages=[{"role": "system", "content": "Answer using the minimum words you can ever use."},
 
284
  )
285
  return response
286
 
287
+ def ryzedb_chat_avatar(question):
288
+ url = "https://inference.dev.ryzeai.ai/chat/stream"
289
+ question = question + ". Summarize and Answer using the minimum words you can ever use."
290
+ payload = json.dumps({
291
+ "input": {
292
+ "chat_history": [],
293
+ "app_id": "af6b2bc719a14478adcff1d71c19dc00",
294
+ "question": question
295
+ },
296
+ "config": {}
297
+ })
298
+ headers = {
299
+ 'Content-Type': 'application/json'
300
+ }
301
+
302
+ try:
303
+ # Send the POST request
304
+ response = requests.request("POST", url, headers=headers, data=payload)
305
+
306
+ # Check for successful request
307
+ response.raise_for_status()
308
+
309
+ # Return the response JSON
310
+ return response.text
311
+
312
+ except requests.exceptions.RequestException as e:
313
+ print(f"An error occurred: {e}")
314
+ return None
315
+
316
 
317
 
318
  @app.route("/run", methods=['POST'])
 
335
  return jsonify({'error': 'Input text prompt cannot be blank'}), 400
336
 
337
  voice_cloning = request.form.get('voice_cloning', 'no')
338
+ chat_model_used = request.form.get('chat_model_used', 'openai')
339
  target_language = request.form.get('target_language', 'original_text')
340
  print('target_language',target_language)
341
  pose_style = int(request.form.get('pose_style', 1))
 
354
  # # response = await translate_text_async(text_prompt, target_language)
355
  # text_prompt = response.choices[0].message.content.strip()
356
 
357
+ if chat_model_used == 'ryzedb':
358
+ response = ryzedb_chat_avatar(text_prompt)
359
+ events = response.split('\r\n\r\n')
360
+ content = None
361
+ for event in events:
362
+ # Split each event block by "\r\n" to get the lines
363
+ lines = event.split('\r\n')
364
+ if len(lines) > 1 and lines[0] == 'event: data':
365
+ # Extract the JSON part from the second line and parse it
366
+ json_data = lines[1].replace('data: ', '')
367
+ try:
368
+ data = json.loads(json_data)
369
+ text_prompt = data.get('content')
370
+ app.config['text_prompt'] = text_prompt
371
+ print('Final output text prompt using ryzedb: ',text_prompt)
372
+ break # Exit the loop once content is found
373
+ except json.JSONDecodeError:
374
+ continue
375
+
376
+ else:
377
+ response = openai_chat_avatar(text_prompt)
378
+ text_prompt = response.choices[0].message.content.strip()
379
+ app.config['text_prompt'] = text_prompt
380
+ print('Final output text prompt using openai: ',text_prompt)
381
 
382
  source_image_path = save_uploaded_file(source_image, 'source_image.png',TEMP_DIR)
383
  print(source_image_path)