Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["XDG_CONFIG_HOME"] = os.path.join(os.getcwd(), ".config")
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import openai
|
| 7 |
+
import time
|
| 8 |
+
from io import StringIO
|
| 9 |
+
|
| 10 |
+
# --- CONFIG ---
|
| 11 |
+
st.set_page_config(page_title="πΌοΈ Alt Text Generator", layout="wide")
|
| 12 |
+
VALID_IMAGE_EXTENSIONS = [".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp"]
|
| 13 |
+
|
| 14 |
+
st.title("πΌοΈ AI Alt Text Generator")
|
| 15 |
+
st.markdown("Generate SEO-friendly alt text for image URLs using GPT-4o Vision.\nMade by [Florian Potier](https://twitter.com/FloPots)")
|
| 16 |
+
|
| 17 |
+
# --- INPUT API KEY ---
|
| 18 |
+
api_key = st.text_input("π Enter your OpenAI API key", type="password")
|
| 19 |
+
if not api_key:
|
| 20 |
+
st.stop()
|
| 21 |
+
client = openai.OpenAI(api_key=api_key)
|
| 22 |
+
|
| 23 |
+
# --- INPUT FILE ---
|
| 24 |
+
uploaded_file = st.file_uploader("π Upload a CSV with image URLs", type=["csv"])
|
| 25 |
+
if uploaded_file:
|
| 26 |
+
df = pd.read_csv(uploaded_file)
|
| 27 |
+
st.success("β
File uploaded successfully")
|
| 28 |
+
st.dataframe(df.head())
|
| 29 |
+
|
| 30 |
+
# --- COLUMN SELECT ---
|
| 31 |
+
image_col = st.selectbox("π§ Select the column with image URLs", df.columns)
|
| 32 |
+
|
| 33 |
+
# --- PROMPT INPUT ---
|
| 34 |
+
prompt_instruction = st.text_area("π Enter the prompt to generate alt text",
|
| 35 |
+
value="Write a concise, SEO-friendly alt text for the image below. Do not use brand names unless visible.")
|
| 36 |
+
|
| 37 |
+
def is_valid_image_url(url: str):
|
| 38 |
+
return any(url.lower().endswith(ext) for ext in VALID_IMAGE_EXTENSIONS)
|
| 39 |
+
|
| 40 |
+
df["Valid Image"] = df[image_col].astype(str).apply(is_valid_image_url)
|
| 41 |
+
valid_df = df[df["Valid Image"] == True]
|
| 42 |
+
|
| 43 |
+
if valid_df.empty:
|
| 44 |
+
st.error("β No valid image URLs found. Make sure URLs end with .jpg, .png, .webp, etc.")
|
| 45 |
+
st.stop()
|
| 46 |
+
|
| 47 |
+
st.info(f"π {len(valid_df)} valid image URLs found. Invalid ones will be skipped.")
|
| 48 |
+
|
| 49 |
+
# --- PROCESS ---
|
| 50 |
+
if st.button("π Start Processing"):
|
| 51 |
+
progress = st.progress(0)
|
| 52 |
+
results = []
|
| 53 |
+
urls = valid_df[image_col].tolist()
|
| 54 |
+
|
| 55 |
+
for idx, url in enumerate(urls):
|
| 56 |
+
try:
|
| 57 |
+
response = client.chat.completions.create(
|
| 58 |
+
model="gpt-4o",
|
| 59 |
+
messages=[
|
| 60 |
+
{
|
| 61 |
+
"role": "user",
|
| 62 |
+
"content": [
|
| 63 |
+
{"type": "text", "text": prompt_instruction},
|
| 64 |
+
{"type": "image_url", "image_url": {"url": url}}
|
| 65 |
+
]
|
| 66 |
+
}
|
| 67 |
+
]
|
| 68 |
+
)
|
| 69 |
+
alt_text = response.choices[0].message.content.strip()
|
| 70 |
+
except Exception as e:
|
| 71 |
+
alt_text = f"ERROR: {e}"
|
| 72 |
+
|
| 73 |
+
results.append(alt_text)
|
| 74 |
+
progress.progress((idx + 1) / len(urls))
|
| 75 |
+
time.sleep(1)
|
| 76 |
+
|
| 77 |
+
valid_df["Generated Alt Text"] = results
|
| 78 |
+
output_df = df.copy()
|
| 79 |
+
output_df.loc[valid_df.index, "Generated Alt Text"] = valid_df["Generated Alt Text"]
|
| 80 |
+
|
| 81 |
+
st.success("β
Done! Preview of the results:")
|
| 82 |
+
st.dataframe(output_df[[image_col, "Generated Alt Text"]].head())
|
| 83 |
+
|
| 84 |
+
csv = output_df.to_csv(index=False).encode("utf-8")
|
| 85 |
+
st.download_button("π₯ Download CSV with Alt Text", csv, "alt_text_output.csv", "text/csv")
|