Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
import torch
|
|
|
1 |
+
'''
|
2 |
+
from datasets import load_dataset
|
3 |
+
from gradio_client import Client, handle_file
|
4 |
+
import os
|
5 |
+
from PIL import Image
|
6 |
+
import tempfile
|
7 |
+
from tqdm import tqdm
|
8 |
+
|
9 |
+
# Load the dataset
|
10 |
+
ds = load_dataset("svjack/Aesthetics_X_Phone_4K_Images_Rec_Captioned")
|
11 |
+
|
12 |
+
# Initialize Gradio client
|
13 |
+
client = Client("http://localhost:7860")
|
14 |
+
|
15 |
+
# Create output directory if it doesn't exist
|
16 |
+
output_dir = "processed_output"
|
17 |
+
os.makedirs(output_dir, exist_ok=True)
|
18 |
+
|
19 |
+
# Iterate through all items in the training set
|
20 |
+
for idx, item in tqdm(enumerate(ds["train"])):
|
21 |
+
try:
|
22 |
+
image = item["image"]
|
23 |
+
joy_caption = item["joy-caption"]
|
24 |
+
|
25 |
+
# Create a temporary file for the input image
|
26 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
27 |
+
temp_image_path = temp_file.name
|
28 |
+
image.save(temp_image_path)
|
29 |
+
|
30 |
+
# Process the image through the API
|
31 |
+
result = client.predict(
|
32 |
+
image=handle_file(temp_image_path),
|
33 |
+
width=1280,
|
34 |
+
height=720,
|
35 |
+
overlap_percentage=10,
|
36 |
+
num_inference_steps=8,
|
37 |
+
resize_option="Full",
|
38 |
+
custom_resize_percentage=50,
|
39 |
+
prompt_input="",
|
40 |
+
alignment="Middle",
|
41 |
+
overlap_left=True,
|
42 |
+
overlap_right=True,
|
43 |
+
overlap_top=True,
|
44 |
+
overlap_bottom=True,
|
45 |
+
api_name="/infer"
|
46 |
+
)
|
47 |
+
|
48 |
+
# Get the processed image path from the result
|
49 |
+
processed_image_path = result[1]
|
50 |
+
|
51 |
+
# Define output paths
|
52 |
+
base_filename = f"processed_{idx}"
|
53 |
+
output_image_path = os.path.join(output_dir, f"{base_filename}.png")
|
54 |
+
output_text_path = os.path.join(output_dir, f"{base_filename}.txt")
|
55 |
+
|
56 |
+
# Ensure the output is saved as PNG
|
57 |
+
if processed_image_path.lower().endswith('.png'):
|
58 |
+
# If already PNG, just copy
|
59 |
+
with Image.open(processed_image_path) as img:
|
60 |
+
img.save(output_image_path, 'PNG')
|
61 |
+
else:
|
62 |
+
# If not PNG, open and convert to PNG
|
63 |
+
with Image.open(processed_image_path) as img:
|
64 |
+
img.save(output_image_path, 'PNG')
|
65 |
+
|
66 |
+
# Save the joy-caption as a text file
|
67 |
+
with open(output_text_path, 'w', encoding='utf-8') as f:
|
68 |
+
f.write(joy_caption)
|
69 |
+
|
70 |
+
print(f"Processed item {idx}: Image saved to {output_image_path}, caption to {output_text_path}")
|
71 |
+
|
72 |
+
except Exception as e:
|
73 |
+
print(f"Error processing item {idx}: {str(e)}")
|
74 |
+
finally:
|
75 |
+
# Clean up temporary files
|
76 |
+
if 'temp_image_path' in locals() and os.path.exists(temp_image_path):
|
77 |
+
os.remove(temp_image_path)
|
78 |
+
|
79 |
+
print("Processing complete!")
|
80 |
+
'''
|
81 |
+
|
82 |
import gradio as gr
|
83 |
import spaces
|
84 |
import torch
|