brekkanegg
no image data
f886efc
# 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()