-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
372 lines (337 loc) · 14.1 KB
/
api.py
File metadata and controls
372 lines (337 loc) · 14.1 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import time
from flask import Flask, request, jsonify
from flask_cors import CORS
import os
from google.cloud import speech
from google.api_core.exceptions import GoogleAPICallError, InvalidArgument
import os.path
import webbrowser
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload
from PIL import Image
from point_maker import get_presentation
from uuid import uuid4
app = Flask(__name__)
CORS(app)
# Specify your desired upload folder path here
# Example: UPLOAD_FOLDER = 'C:/Users/YourUsername/Documents/AudioUploads'
UPLOAD_FOLDER = "./sound_files"
ALLOWED_EXTENSIONS = {'mp3', 'wav', 'ogg'}
# If modifying these scopes, delete the file token.json.
SCOPES = [
"https://www.googleapis.com/auth/presentations",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"
]
# The ID of a sample presentation.
PRESENTATION_ID = "1OOvlh9s-sd5PHptLi0N3NGKxKYxNdOcPZLbcGcjL-go"
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def resize_and_convert(image_path, size=(600, 450)):
"""Resize and convert image to a supported format (PNG)."""
img = Image.open(image_path)
img = img.resize(size)
new_image_path = os.path.splitext(image_path)[0] + ".png"
img.save(new_image_path, "PNG")
return new_image_path
def slides():
"""Shows basic usage of the Slides API.
Prints the number of slides and elements in a sample presentation.
"""
creds = None
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=8000)
with open("token.json", "w") as token:
token.write(creds.to_json())
try:
service = build("slides", "v1", credentials=creds)
presentation = service.presentations().get(presentationId=PRESENTATION_ID).execute()
slides = presentation.get("slides")
if slides is not None:
print(f"The presentation contains {len(slides)} slides:")
else:
print("No slides found in the presentation.")
# Create title slide
title_slide_request = [
{
"createSlide": {
"objectId": "titleSlide",
"insertionIndex": 0,
"slideLayoutReference": {
"predefinedLayout": "BLANK"
}
}
},
{
"createShape": {
"objectId": "titleTextBox",
"shapeType": "TEXT_BOX",
"elementProperties": {
"pageObjectId": "titleSlide",
"size": {
"height": {
"magnitude": 1000000,
"unit": "EMU"
},
"width": {
"magnitude": 6000000,
"unit": "EMU"
}
},
"transform": {
"scaleX": 1,
"scaleY": 1,
"translateX": 7200000 / 2 - 6000000 / 2, # Subtract half of the width from half of the slide's width
"translateY": 4050000 / 2 - 1000000 / 2, # Subtract half of the height from half of the slide's height
"unit": "EMU"
}
}
}
},
{
"insertText": {
"objectId": "titleTextBox",
"insertionIndex": 0,
"text": presentation_text[0]
}
},
{
"updateTextStyle": {
"objectId": "titleTextBox",
"style": {
"bold": True,
"fontSize": {
"magnitude": 50, # Increase the font size
"unit": "PT"
}
},
"textRange": {
"type": "ALL"
},
"fields": "bold,fontSize"
}
}
]
service.presentations().batchUpdate(presentationId=PRESENTATION_ID, body={"requests": title_slide_request}).execute()
drive_service = build('drive', 'v3', credentials=creds)
for i, slide_content in enumerate(presentation_text[1:], start=1):
slide_id = f"slide{i}"
title_id = f"title{i}"
slide_request = [
{
"createSlide": {
"objectId": slide_id,
"insertionIndex": i,
"slideLayoutReference": {
"predefinedLayout": "BLANK"
}
}
},
{
"createShape": {
"objectId": title_id,
"shapeType": "TEXT_BOX",
"elementProperties": {
"pageObjectId": slide_id,
"size": {
"height": {
"magnitude": 1000000,
"unit": "EMU"
},
"width": {
"magnitude": 7200000, # Make the textbox as long as the width
"unit": "EMU"
}
},
"transform": {
"scaleX": 1,
"scaleY": 1,
"translateX": 0, # Position the textbox at the left of the screen
"translateY": 0, # Position the textbox at the top of the screen
"unit": "EMU"
}
}
}
},
{
"insertText": {
"objectId": title_id,
"insertionIndex": 0,
"text": slide_content[0] # Use the first string in each element as the title
}
},
{
"updateTextStyle": {
"objectId": title_id,
"style": {
"bold": True,
"fontSize": {
"magnitude": 30, # Decrease the font size
"unit": "PT"
}
},
"textRange": {
"type": "ALL"
},
"fields": "bold,fontSize"
}
}
]
# Create a new text box for each bullet point
for j, bullet_point in enumerate(slide_content[1:], start=1):
bullet_id = f"bullet{i}_{j}"
bullet_request = [
{
"createShape": {
"objectId": bullet_id,
"shapeType": "TEXT_BOX",
"elementProperties": {
"pageObjectId": slide_id,
"size": {
"height": {
"magnitude": 1000000,
"unit": "EMU"
},
"width": {
"magnitude": 5000000,
"unit": "EMU"
}
},
"transform": {
"scaleX": 1,
"scaleY": 1,
"translateX": 0, # Position the textbox at the left of the screen
"translateY": 700000 * j, # Position the textbox below the previous one with less gap
"unit": "EMU"
}
}
}
},
{
"insertText": {
"objectId": bullet_id,
"insertionIndex": 0,
"text": f"• {bullet_point}" # Add a bullet point before the text
}
},
{
"updateTextStyle": {
"objectId": bullet_id,
"style": {
"fontSize": {
"magnitude": 15, # Decrease the font size more
"unit": "PT"
}
},
"textRange": {
"type": "ALL"
},
"fields": "fontSize"
}
},
]
slide_request.extend(bullet_request)
# Upload the image to Google Drive
image_path = f"{i}.png"
if os.path.exists(image_path):
image = resize_and_convert(image_path)
drive_service = build("drive", "v3", credentials=creds)
file_metadata = {
"name": image,
"mimeType": "image/png"
}
media = MediaFileUpload(image, mimetype="image/png", resumable=True)
image_file = drive_service.files().create(body=file_metadata, media_body=media, fields="id").execute()
drive_service.permissions().create(
fileId=image_file['id'],
body={
'type': 'anyone',
'role': 'reader',
},
fields='id',
).execute()
image_id = f"image_{uuid4().hex}" # Generate unique image ID
slide_request.append({
"createImage": {
"objectId": image_id,
"url": f"https://drive.google.com/uc?id={image_file['id']}",
"elementProperties": {
"pageObjectId": slide_id,
"size": {
"height": {
"magnitude": 2812500,
"unit": "EMU"
},
"width": {
"magnitude": 3750000,
"unit": "EMU"
}
},
"transform": {
"scaleX": 1,
"scaleY": 1,
"translateX": 5297000,
"translateY": 1265625,
"unit": "EMU"
}
}
}
})
service.presentations().batchUpdate(presentationId=PRESENTATION_ID, body={"requests": slide_request}).execute()
webbrowser.open(f"https://docs.google.com/presentation/d/{PRESENTATION_ID}/edit")
except HttpError as err:
print(err)
@app.route('/upload', methods=['POST'])
def upload_file():
if 'audio' not in request.files:
return jsonify({'error': 'No audio file part'}), 400
file = request.files['audio']
if file.filename == '':
return jsonify({'error': 'No audio file selected'}), 400
if file and allowed_file(file.filename):
filename = file.filename
# Ensure the UPLOAD_FOLDER exists
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
file_path = os.path.join(UPLOAD_FOLDER, filename)
file.save(file_path)
time.sleep(5)
client = speech.SpeechClient.from_service_account_file('./cloud_key.json')
try:
with open(file_path, "rb") as f:
data = f.read()
audio_file = speech.RecognitionAudio(content=data)
config = speech.RecognitionConfig(
sample_rate_hertz=44100,
encoding=speech.RecognitionConfig.AudioEncoding.MP3,
language_code='en-US',
enable_automatic_punctuation=True
)
response = client.recognize(config=config, audio=audio_file)
sentence = response.results[0].alternatives[0].transcript
global presentation_text
presentation_text = get_presentation(sentence)
slides()
return sentence
except GoogleAPICallError as e:
print(f"An error occurred during the API call: {e}")
return e
except InvalidArgument as e:
print(f"Invalid argument passed to the API call: {e}")
return e
except Exception as e:
print(f"An unexpected error occurred: {e}")
return e
else:
return jsonify({'error': 'File type not allowed'}), 400
if __name__ == '__main__':
app.run(debug=True, port=3000)