-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_server.py
More file actions
134 lines (116 loc) · 4.14 KB
/
api_server.py
File metadata and controls
134 lines (116 loc) · 4.14 KB
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
import traceback
from flask import Flask, request, send_file
from flask_cors import CORS
import os
import shutil
import tempfile
import subprocess
import traceback
from git import Repo
import requests
from pathlib import Path
from temprunner import generate_slides, process_code_directory
from temprunner import extract_text_from_pdf as extract_text_code_main
from drive_extract import DriveDownloader
from extract_links import download_from_github
from extract_text_code import process_code, process_text, process_code_wrapper, process_text_wrapper
from imgextract import process_directory
from marp_slides import create_presentation_graph
import time
from graphs.process import image_gen
app = Flask(__name__)
CORS(app)
def process_drive_data(drive_url):
downloader = DriveDownloader()
try:
downloader.download_folder(drive_url, 'text_data')
# Convert async functions to sync
process_text_wrapper()
process_directory()
return True
except Exception as e:
print(f"Error in drive processing: {e}")
return False
def process_github_data(github_url):
try:
download_from_github(github_url)
# Convert async process_code to sync
process_code_wrapper()
shutil.rmtree('code_data', ignore_errors=True)
return True
except Exception as e:
print(f"Error in github processing: {e}")
return False
def create_slides(prompt):
try:
topic = "Sales pitch"
# Read summaries
text_summary = ""
code_summary = ""
with open("extract/summary/text_summary.txt", "r") as file:
text_summary = file.read()
with open("extract/summary/code_summary.txt", "r") as file:
code_summary = file.read()
# Create and process graph
graph = create_presentation_graph()
result = graph.invoke({
"topic": topic,
"requirements": prompt,
"text_context": text_summary,
"code_context": code_summary
})
# Create markdown and convert to HTML
with open("presentation.md", "w") as file:
file.write(result['slides']['marp_markdown'])
output_html = "pitcher/public/presentation.html"
subprocess.run(["marp", "presentation.md", "-o", output_html], check=True)
time.sleep(2)
return True
except Exception as e:
print(f"Error in slide creation: {e}")
print(traceback.format_exc())
return False
@app.route('/api/projects', methods=['POST'])
def process_project():
try:
start_time = time.time()
data = request.json
prompt = data.get('prompt')
github_url = data.get('githubUrl')
drive_url = data.get('driveUrl')
if not any([prompt, github_url, drive_url]):
return {'error': 'No content to process'}, 400
# Process sequentially instead of concurrently
if drive_url:
process_drive_data(drive_url)
if github_url:
process_github_data(github_url)
# Generate images
image_gen()
if prompt:
create_slides(prompt)
time.sleep(2)
print("Time_take: ", time.time() - start_time)
return {'message': 'All processes completed successfully',
'filename': 'presentation.html'}, 200
except Exception as e:
print("An error occurred:", e)
print(traceback.format_exc())
return {'error': str(e)}, 500
@app.route('/api/presentation-status', methods=['GET'])
def find_status():
try:
with open("status.txt", "r") as file:
content = file.read()
if "Move" in content:
with open('status.txt', 'w') as f:
f.write("not ready")
return {'status': 'Yes'}, 200
else:
return {'status': 'Wait'}, 200
except FileNotFoundError:
return {'error': 'status.txt file not found'}, 404
if __name__ == '__main__':
print("Starting server...")
# Use regular Flask development server instead of hypercorn
app.run(host='localhost', port=5000, debug=True)