Spaces:
Sleeping
Sleeping
# app.py | |
import gradio as gr | |
import pandas as pd | |
from PIL import Image | |
import io | |
import requests | |
import base64 | |
def process_excel(file): | |
# Check if the file is None | |
if file is None: | |
return "No file uploaded.", pd.DataFrame() | |
# Read the uploaded Excel file into a DataFrame | |
try: | |
df = pd.read_excel(file.name, engine='openpyxl') | |
except Exception as e: | |
return f"Error reading Excel file: {str(e)}", pd.DataFrame() | |
# Extract card information | |
cards = [] | |
for index, row in df.iterrows(): | |
if row['์ฌ์ฉ์ฌ๋ถ'] != 'Y': | |
continue | |
image_url = row['์ฌ์ง'] | |
try: | |
response = requests.get(image_url) | |
response.raise_for_status() # Ensure we notice bad responses | |
# Validate if the response contains image data | |
if 'image' not in response.headers.get('Content-Type', ''): | |
raise ValueError("URL does not point to an image") | |
image = Image.open(io.BytesIO(response.content)) | |
with io.BytesIO() as output: | |
image.save(output, format="PNG") | |
image_data = base64.b64encode(output.getvalue()).decode('utf-8') | |
except Exception as e: | |
print(f"Error processing image from URL {image_url}: {str(e)}") | |
image_data = None | |
# Create card layout | |
card = f""" | |
<div style="width: 800px; height: 400px; border: 1px solid #333; margin: 10px auto; background-color: #2E6A8C; color: white; font-family: Arial, sans-serif; display: flex; flex-direction: row;"> | |
<div style="text-align: center; padding: 10px; font-size: 18px; border-bottom: 1px solid white;"> | |
{row['์ ํ๋ช ']} / {row['ํ๋งค์']} | |
</div> | |
<div></div> | |
<div style="display: flex; flex-direction: col;"> | |
<img src="data:image/png;base64,{image_data}" alt="Image" style="max-width: 50%; max-height: 100%; object-fit: cover; border-right: 1px solid white;"> | |
<div style="flex; text-align: center; padding: 10px;"> | |
<div style="margin-bottom: 10px;">์ฃผ์ฑ๋ถ: {row['์ฃผ์์ฑ๋ถ']}</div> | |
<div style="margin-bottom: 10px;">1ํ ์ฉ๋: {row['1ํ ์ฉ๋']}</div> | |
<div style="margin-bottom: 10px;">1์ผ ๋ณต์ฉ ํ์: {row['1์ผ ๋ณต์ฉํ์']}</div> | |
<div>๊ท๊ฒฉ: {row['๊ท๊ฒฉ']}</div> | |
</div> | |
</div> | |
</div> | |
""" | |
cards.append(card) | |
return df, "\n\n".join(cards) | |
iface = gr.Interface( | |
fn=process_excel, | |
inputs=gr.File(type="filepath", label="excel ํ์ผ์ ์ ๋ก๋ํด์ฃผ์ธ์."), | |
outputs=[gr.Dataframe(label="์ ๋ ฅ๊ฐ"), gr.HTML(label="๋ธ๋ก์ ")], | |
live=True | |
) | |
iface.launch() | |
iface = gr.Interface( | |
fn=process_excel, | |
inputs=gr.File(type="filepath", label="excel ํ์ผ์ ์ ๋ก๋ํด์ฃผ์ธ์."), | |
# outputs=gr.Markdown(), | |
outputs=[gr.Dataframe(label="์ ๋ ฅ๊ฐ"), gr.HTML(label="๋ธ๋ก์ ")], | |
live=True | |
) | |
iface.launch() | |