File size: 5,350 Bytes
255df7f
f8e3a12
8e3c369
96f419b
b8e8579
96f419b
8e3c369
e738b55
1125603
8e3c369
 
d8b35ea
255df7f
87f10b8
36d77a8
f6c296e
 
255df7f
2f55002
b8e8579
 
2f55002
b8e8579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e3c369
 
 
ea26699
b8e8579
 
ea26699
3f6bd82
3bde6e5
 
9b19eb5
1125603
8e3c369
 
1125603
 
 
 
 
36e3155
1125603
ebd1c2f
 
 
1125603
 
 
 
ebd1c2f
1125603
 
ebd1c2f
e9902d8
 
1125603
 
 
e9902d8
1125603
e9902d8
ebd1c2f
1125603
 
e9902d8
 
1125603
 
8c36521
 
1125603
8c36521
1125603
ebd1c2f
1125603
e9902d8
 
1125603
e9902d8
 
1125603
 
 
e9902d8
1125603
e9902d8
 
 
1125603
e9902d8
 
1125603
 
f29c874
36d77a8
 
 
 
 
 
f29c874
 
36d77a8
 
f29c874
e738b55
dbca55f
2f9786d
2f55002
 
 
b8e8579
f29c874
 
 
 
3bde6e5
89193d4
3bde6e5
3f6bd82
87f10b8
1125603
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from flask import Flask, request, jsonify, send_file
from moviepy.editor import ColorClip,ImageClip, concatenate_videoclips,VideoFileClip
import traceback
import uuid
from concurrent.futures import ThreadPoolExecutor, as_completed
import glob
import os
import requests
import asyncio
from image_fetcher import main
from video import create_text_image
from video2 import video_com,video_func

app = Flask(__name__)

SAVE_DIR = "/app/data/video/"
os.makedirs(SAVE_DIR, exist_ok=True)

def send_task(id,url,lines):
    payload = {
        "id": id,
        "lines": lines  
    }

    try:
        response = requests.post(url, json=payload)
        if response.status_code == 200:
            video_path = f"/app/data/video/clip{id}.mp4"
            with open(video_path, "wb") as f:
                f.write(response.content)
            print(video_path)
        else:
            raise Exception(f"[{url}] HTTP {response.status_code}: {response.text}")
    except Exception as e:
        print(f"Error for ID {id}: {e}")
        return None


@app.route("/")
def home():
    return "Flask Video Generator is Running"



@app.route("/generate", methods=["POST"])
def generate_video():
    try:
        data = request.get_json()
        prompt = data.get("duration", '').strip()
        prompts=prompt.replace("**","")
        print(prompts)
        if prompts == '':
            return jsonify({"error": "prompts be must"}), 400
        image_folder = "/tmp/images"
        #line=prompts.splitlines()
        #asyncio.run(main(line))
        raw_lines = prompts.splitlines(keepends=False)
        lines = []
        
        i = 0
        while i < len(raw_lines):
            line = raw_lines[i].strip()
        
            # Check if current line is a heading
            if line.strip().startswith("#") and (line.endswith('?') or line.endswith(':')):
                block = line  # Start block with heading
                i += 1
        
                # Accumulate body lines until next heading or 5+ lines
                paragraph_lines = []
                while i < len(raw_lines):
                    next_line = raw_lines[i].strip()
        
                    # Stop if next line is a heading
                    if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
                        break
        
                    paragraph_lines.append(next_line)
                    i += 1
        
                    # If we've gathered enough lines for a slide, break to next
                    if len(paragraph_lines) >= 5:
                        break
        
                # Combine heading + paragraph
                if paragraph_lines:
                    block += '\n' + '\n'.join(paragraph_lines)
        
                lines.append(block)
        
            else:
                # Group normal lines (not part of any heading)
                block_lines = []
                count = 0
        
                while i < len(raw_lines) and count < 5:
                    next_line = raw_lines[i].strip()
        
                    # If this is a heading, break to handle it separately
                    if next_line.strip().startswith("#") and (next_line.endswith('?') or next_line.endswith(':')):
                        break
        
                    block_lines.append(next_line)
                    i += 1
                    count += 1
        
                if block_lines:
                    lines.append('\n'.join(block_lines))
        
        # Print or use lines as slides
        if len(lines)==1:
            image_folder="/tmp/images"
            image_olst=[]
            create_text_image(lines[0],0,image_olst)
            image_files = sorted(glob.glob(os.path.join(image_folder, "*.png")))
            if not image_files:
                raise ValueError("No images found in folder!")
            video_link = "/app/data/video/clip0.mp4"
            video_func(0,lines)
            for img in image_files:
                os.remove(img)
            return send_file(video_link, mimetype='video/mp4')
        else:
            space_b_url = ['https://sreepathi-ravikumar-subprocess1.hf.space/generate', 'https://sreepathi-ravikumar-subprocess2.hf.space/generate', 'https://sreepathi-ravikumar-subprocess3.hf.space/generate', 'https://sreepathi-ravikumar-subprocess4.hf.space/generate', 'https://sreepathi-ravikumar-subprocess5.hf.space/generate', 'https://sreepathi-ravikumar-subprocess6.hf.space/generate', 'https://sreepathi-ravikumar-subprocess7.hf.space/generate', 'https://sreepathi-ravikumar-subprocess8.hf.space/generate', 'https://sreepathi-ravikumar-subprocess9.hf.space/generate', 'https://sreepathi-ravikumar-subprocess10.hf.space/generate']

            with ThreadPoolExecutor(max_workers=len(lines)) as executor:
                for id in range(len(lines)):
                    executor.submit(send_task,id,space_b_url[id],lines)

            video_path = video_com(lines)
            #for img in image_files:
                #os.remove(img)
            return send_file(video_path, mimetype='video/mp4')
    except Exception as e:
        traceback.print_exc()
        return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)

    # Example call (remove or change in your actual app)