sreepathi-ravikumar commited on
Commit
abe15dd
·
verified ·
1 Parent(s): 1b3ef2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -81
app.py CHANGED
@@ -1,106 +1,42 @@
1
- from flask import Flask, request, jsonify, send_file
2
  from flask_cors import CORS
3
- from moviepy.editor import ImageClip, concatenate_videoclips
4
- import traceback
5
- import uuid
6
- import glob
7
- import os
8
- import asyncio
9
 
10
- from text2generation import generate_long_answer # Your function
11
- from image_fetcher import main # Your async function (optional)
12
- from video import create_text_image # Your function
13
-
14
- app = Flask(__name__)
15
  CORS(app)
16
-
17
- # Store temporary data
18
- temp_data = {}
19
 
20
  @app.route('/health')
21
  def health_check():
22
  return jsonify({"status": "ready"}), 200
23
 
 
24
  @app.route('/ask', methods=['POST'])
25
  def ask_question():
26
  try:
 
27
  data = request.get_json()
28
  question = data.get('question', '').strip()
29
 
30
  if not question:
31
  return jsonify({"error": "Question is required"}), 400
32
 
33
- # Step 1: Generate Answer
34
  answer = generate_long_answer(question)
35
- temp_data['answer'] = answer
36
-
37
- # Step 2: Start Video Generation
38
- video_path = generate_video_from_text(answer)
39
- temp_data['video_path'] = video_path
40
-
41
- # Step 3: Respond with JSON
42
- return jsonify({
43
- "question": question,
44
- "answer": answer,
45
- "video_url": f"/download_video/{os.path.basename(video_path)}"
46
- })
47
-
48
- except Exception as e:
49
- traceback.print_exc()
50
- return jsonify({"error": str(e)}), 500
51
 
52
- @app.route('/download_video/<filename>')
53
- def download_video(filename):
54
- try:
55
- path = f"/tmp/{filename}"
56
- return send_file(path, mimetype='video/mp4')
57
  except Exception as e:
58
  return jsonify({"error": str(e)}), 500
59
 
60
- def generate_video_from_text(prompts):
61
- image_folder = "/tmp/images"
62
- os.makedirs(image_folder, exist_ok=True)
63
-
64
- raw_lines = prompts.splitlines(keepends=False)
65
- lines = []
66
-
67
- i = 0
68
- while i < len(raw_lines):
69
- line = raw_lines[i].strip()
70
- if line.endswith('?') or line.endswith(';'):
71
- block = line
72
- i += 1
73
- paragraph_lines = []
74
- count = 0
75
- while i < len(raw_lines) and not (raw_lines[i].strip().endswith('?') or raw_lines[i].strip().endswith(';')) and count < 5:
76
- paragraph_lines.append(raw_lines[i])
77
- i += 1
78
- count += 1
79
- if paragraph_lines:
80
- block += '\n' + '\n'.join(paragraph_lines)
81
- lines.append(block)
82
- else:
83
- block = '\n'.join(raw_lines[i:i+5])
84
- lines.append(block)
85
- i += 5
86
-
87
- image_list = []
88
- for idx, block in enumerate(lines):
89
- create_text_image(block, idx, image_list)
90
-
91
- image_files = sorted(glob.glob(os.path.join(image_folder, "*.png")))
92
- if not image_files:
93
- raise ValueError("No images found!")
94
-
95
- clips = [ImageClip(img).set_duration(5) for img in image_files]
96
- video = concatenate_videoclips(clips, method="compose")
97
- video_path = f"/tmp/video_{uuid.uuid4().hex}.mp4"
98
- video.write_videofile(video_path, fps=24)
99
-
100
- for img in image_files:
101
- os.remove(img)
102
 
103
- return video_path
104
 
105
- if __name__ == '__main__':
106
  app.run(host='0.0.0.0', port=7860)
 
1
+ from flask import Flask, request, jsonify, Response,redirect
2
  from flask_cors import CORS
3
+ import time
4
+ import random
5
+ from text2generation import generate_long_answer # Your own function
 
 
 
6
 
7
+ app = Flask(_name_)
 
 
 
 
8
  CORS(app)
9
+ temp_data={}
 
 
10
 
11
  @app.route('/health')
12
  def health_check():
13
  return jsonify({"status": "ready"}), 200
14
 
15
+ # Standard answer route
16
  @app.route('/ask', methods=['POST'])
17
  def ask_question():
18
  try:
19
+ global temp_data
20
  data = request.get_json()
21
  question = data.get('question', '').strip()
22
 
23
  if not question:
24
  return jsonify({"error": "Question is required"}), 400
25
 
 
26
  answer = generate_long_answer(question)
27
+ print(answer)
28
+ temp_data['answered']=answer
29
+ return jsonify({"question": question, "answer": answer})
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
 
 
 
 
 
31
  except Exception as e:
32
  return jsonify({"error": str(e)}), 500
33
 
34
+ @app.route('/get_video')
35
+ def get_video():
36
+ video_path=temp_data.get("answered")
37
+ print(video_path)
38
+ return redirect()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
40
 
41
+ if _name_ == '_main_':
42
  app.run(host='0.0.0.0', port=7860)