Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -176,9 +176,6 @@ def create_file(filename, prompt, response, is_image=False):
|
|
176 |
with open(filename, "w", encoding="utf-8") as f:
|
177 |
f.write(prompt + "\n\n" + response)
|
178 |
|
179 |
-
def save_image_old2(image, filename):
|
180 |
-
with open(filename, "wb") as f:
|
181 |
-
f.write(image.getbuffer())
|
182 |
|
183 |
# Now filename length protected for linux and windows filename lengths
|
184 |
def save_image(image, filename):
|
@@ -261,47 +258,6 @@ def process_image(image_input, user_prompt):
|
|
261 |
|
262 |
return image_response
|
263 |
|
264 |
-
def process_image_old(image_input, user_prompt):
|
265 |
-
if image_input:
|
266 |
-
st.markdown('Processing image: ' + image_input.name )
|
267 |
-
if image_input:
|
268 |
-
base64_image = base64.b64encode(image_input.read()).decode("utf-8")
|
269 |
-
response = client.chat.completions.create(
|
270 |
-
model=MODEL,
|
271 |
-
messages=[
|
272 |
-
{"role": "system", "content": "You are a helpful assistant that responds in Markdown."},
|
273 |
-
{"role": "user", "content": [
|
274 |
-
{"type": "text", "text": user_prompt},
|
275 |
-
{"type": "image_url", "image_url": {
|
276 |
-
"url": f"data:image/png;base64,{base64_image}"}
|
277 |
-
}
|
278 |
-
]}
|
279 |
-
],
|
280 |
-
temperature=0.0,
|
281 |
-
)
|
282 |
-
image_response = response.choices[0].message.content
|
283 |
-
st.markdown(image_response)
|
284 |
-
|
285 |
-
# Save markdown on image AI output from gpt4o
|
286 |
-
filename_md = generate_filename(image_input.name + '- ' + image_response, "md")
|
287 |
-
# Save markdown on image AI output from gpt4o
|
288 |
-
filename_png = filename_md.replace('.md', '.' + image_input.name.split('.')[-1])
|
289 |
-
|
290 |
-
create_file(filename_md, image_response, '', True) #create_file() # create_file() 3 required positional arguments: 'filename', 'prompt', and 'response'
|
291 |
-
|
292 |
-
with open(filename_md, "w", encoding="utf-8") as f:
|
293 |
-
f.write(image_response)
|
294 |
-
|
295 |
-
# Extract boldface terms from image_response then autoname save file
|
296 |
-
#boldface_terms = extract_boldface_terms(image_response)
|
297 |
-
boldface_terms = extract_title(image_response).replace(':','')
|
298 |
-
filename_stem, extension = os.path.splitext(image_input.name)
|
299 |
-
filename_img = f"{filename_stem} {''.join(boldface_terms)}{extension}"
|
300 |
-
newfilename = save_image(image_input, filename_img)
|
301 |
-
filename_md = newfilename.replace('.png', '.md')
|
302 |
-
create_file(filename_md, '', image_response, True)
|
303 |
-
|
304 |
-
return image_response
|
305 |
|
306 |
def create_audio_file(filename, audio_data, should_save):
|
307 |
if should_save:
|
@@ -1172,28 +1128,6 @@ def display_glossary(glossary, area):
|
|
1172 |
st.write(f"{idx}. {term}")
|
1173 |
|
1174 |
|
1175 |
-
|
1176 |
-
#@st.cache_resource
|
1177 |
-
def display_videos_and_links_old(num_columns):
|
1178 |
-
video_files = [f for f in os.listdir('.') if f.endswith('.mp4')]
|
1179 |
-
if not video_files:
|
1180 |
-
st.write("No MP4 videos found in the current directory.")
|
1181 |
-
return
|
1182 |
-
|
1183 |
-
video_files_sorted = sorted(video_files, key=lambda x: len(x.split('.')[0]))
|
1184 |
-
cols = st.columns(num_columns) # Define num_columns columns outside the loop
|
1185 |
-
col_index = 0 # Initialize column index
|
1186 |
-
|
1187 |
-
for video_file in video_files_sorted:
|
1188 |
-
with cols[col_index % num_columns]: # Use modulo 2 to alternate between the first and second column
|
1189 |
-
# Embedding video with autoplay and loop using HTML
|
1190 |
-
#video_html = ("""<video width="100%" loop autoplay> <source src="{video_file}" type="video/mp4">Your browser does not support the video tag.</video>""")
|
1191 |
-
#st.markdown(video_html, unsafe_allow_html=True)
|
1192 |
-
k = video_file.split('.')[0] # Assumes keyword is the file name without extension
|
1193 |
-
st.video(video_file, format='video/mp4', start_time=0)
|
1194 |
-
display_glossary_entity(k)
|
1195 |
-
col_index += 1 # Increment column index to place the next video in the next column
|
1196 |
-
|
1197 |
def display_images_and_wikipedia_summaries(num_columns=4):
|
1198 |
image_files = [f for f in os.listdir('.') if f.endswith('.png')]
|
1199 |
if not image_files:
|
@@ -1246,50 +1180,6 @@ def display_videos_and_links(num_columns):
|
|
1246 |
|
1247 |
col_index += 1 # Increment column index to place the next video in the next column
|
1248 |
|
1249 |
-
def display_videos_and_links_old2(num_columns):
|
1250 |
-
video_files = [f for f in os.listdir('.') if f.endswith('.mp4')]
|
1251 |
-
if not video_files:
|
1252 |
-
st.write("No MP4 videos found in the current directory.")
|
1253 |
-
return
|
1254 |
-
|
1255 |
-
video_files_sorted = sorted(video_files, key=lambda x: len(x.split('.')[0]))
|
1256 |
-
cols = st.columns(num_columns) # Define num_columns columns outside the loop
|
1257 |
-
col_index = 0 # Initialize column index
|
1258 |
-
|
1259 |
-
for video_file in video_files_sorted:
|
1260 |
-
with cols[col_index % num_columns]: # Use modulo to alternate between columns
|
1261 |
-
k = video_file.split('.')[0] # Assumes keyword is the file name without extension
|
1262 |
-
st.video(video_file, format='video/mp4', start_time=0)
|
1263 |
-
display_glossary_entity(k)
|
1264 |
-
|
1265 |
-
# Add text input for video file
|
1266 |
-
video_text_input = st.text_input(f"Video Prompt for {video_file}", key=f"video_prompt_{video_file}")
|
1267 |
-
if video_text_input:
|
1268 |
-
process_video(video_file, video_text_input)
|
1269 |
-
|
1270 |
-
col_index += 1 # Increment column index to place the next video in the next column
|
1271 |
-
|
1272 |
-
#@st.cache_resource
|
1273 |
-
def display_images_and_wikipedia_summaries_old(num_columns=4):
|
1274 |
-
image_files = [f for f in os.listdir('.') if f.endswith('.png')]
|
1275 |
-
if not image_files:
|
1276 |
-
st.write("No PNG images found in the current directory.")
|
1277 |
-
return
|
1278 |
-
|
1279 |
-
image_files_sorted = sorted(image_files, key=lambda x: len(x.split('.')[0]))
|
1280 |
-
|
1281 |
-
cols = st.columns(num_columns) # Use specified num_columns for layout
|
1282 |
-
col_index = 0 # Initialize column index for cycling through columns
|
1283 |
-
|
1284 |
-
for image_file in image_files_sorted:
|
1285 |
-
with cols[col_index % num_columns]: # Cycle through columns based on num_columns
|
1286 |
-
image = Image.open(image_file)
|
1287 |
-
st.image(image, caption=image_file, use_column_width=True)
|
1288 |
-
k = image_file.split('.')[0] # Assumes keyword is the file name without extension
|
1289 |
-
display_glossary_entity(k)
|
1290 |
-
col_index += 1 # Increment to move to the next column in the next iteration
|
1291 |
-
|
1292 |
-
|
1293 |
def get_all_query_params(key):
|
1294 |
return st.query_params().get(key, [])
|
1295 |
|
|
|
176 |
with open(filename, "w", encoding="utf-8") as f:
|
177 |
f.write(prompt + "\n\n" + response)
|
178 |
|
|
|
|
|
|
|
179 |
|
180 |
# Now filename length protected for linux and windows filename lengths
|
181 |
def save_image(image, filename):
|
|
|
258 |
|
259 |
return image_response
|
260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
|
262 |
def create_audio_file(filename, audio_data, should_save):
|
263 |
if should_save:
|
|
|
1128 |
st.write(f"{idx}. {term}")
|
1129 |
|
1130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1131 |
def display_images_and_wikipedia_summaries(num_columns=4):
|
1132 |
image_files = [f for f in os.listdir('.') if f.endswith('.png')]
|
1133 |
if not image_files:
|
|
|
1180 |
|
1181 |
col_index += 1 # Increment column index to place the next video in the next column
|
1182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1183 |
def get_all_query_params(key):
|
1184 |
return st.query_params().get(key, [])
|
1185 |
|