Update app.py
Browse files
app.py
CHANGED
|
@@ -18,14 +18,11 @@ Date: June 2025
|
|
| 18 |
import os
|
| 19 |
import base64
|
| 20 |
import json
|
| 21 |
-
import asyncio
|
| 22 |
-
import aiohttp
|
| 23 |
from datetime import datetime, timedelta
|
| 24 |
from typing import List, Dict, Optional, Any
|
| 25 |
import io
|
| 26 |
import re
|
| 27 |
from dataclasses import dataclass
|
| 28 |
-
from urllib.parse import quote_plus
|
| 29 |
import functools
|
| 30 |
import gc
|
| 31 |
|
|
@@ -33,17 +30,12 @@ import gc
|
|
| 33 |
if not os.environ.get("HF_TOKEN"):
|
| 34 |
print("β οΈ HF_TOKEN not found - translation features will use fallback mode")
|
| 35 |
|
| 36 |
-
|
| 37 |
-
os.environ["SAMBANOVA_API_KEY"] = "7f3e8b92-3171-4927-a250-14e3a7e01a9d"
|
| 38 |
|
| 39 |
# Core libraries
|
| 40 |
import gradio as gr
|
| 41 |
-
from PIL import Image
|
| 42 |
-
import numpy as np
|
| 43 |
-
import pandas as pd
|
| 44 |
from textblob import TextBlob
|
| 45 |
-
import requests
|
| 46 |
-
from bs4 import BeautifulSoup
|
| 47 |
|
| 48 |
# OpenAI for SambaNova
|
| 49 |
import openai
|
|
@@ -792,6 +784,121 @@ class AdvancedInstagramGenerator:
|
|
| 792 |
|
| 793 |
return ''.join(caption_parts)
|
| 794 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 795 |
async def generate_advanced_caption(self, images: List[Image.Image], style: str,
|
| 796 |
audience: str, custom_prompt: str = "") -> str:
|
| 797 |
"""Generate advanced caption with SambaNova integration"""
|
|
@@ -945,6 +1052,65 @@ async def generate_advanced_caption_interface(uploaded_files, style, audience,
|
|
| 945 |
return result, caption_only
|
| 946 |
|
| 947 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 948 |
async def translate_caption_interface(base_caption, selected_languages):
|
| 949 |
"""Generate multi-language versions of captions"""
|
| 950 |
if not base_caption.strip():
|
|
@@ -1044,26 +1210,96 @@ async def translate_caption_interface(base_caption, selected_languages):
|
|
| 1044 |
|
| 1045 |
|
| 1046 |
def create_gradio_app():
|
| 1047 |
-
"""Create the
|
| 1048 |
|
| 1049 |
# Status indicators
|
| 1050 |
hf_status = "β
Connected" if generator and generator.hf_client_working else "β οΈ Fallback Mode"
|
| 1051 |
sambanova_status = "β
Connected" if generator and generator.sambanova_client_working else "β οΈ Fallback Mode"
|
| 1052 |
|
| 1053 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1054 |
|
| 1055 |
# Main Header
|
| 1056 |
gr.HTML(f"""
|
| 1057 |
-
<div
|
| 1058 |
-
<h1 style="font-size: 2.5rem; margin-bottom: 15px; font-weight: 800;">
|
| 1059 |
π± INSTAGRAM CAPTION GENERATOR
|
| 1060 |
</h1>
|
| 1061 |
<h2 style="font-size: 1.2rem; margin-bottom: 20px; opacity: 0.9;">
|
| 1062 |
π AI-Powered Content Creation β’ SambaNova + Hugging Face
|
| 1063 |
</h2>
|
| 1064 |
<div style="display: flex; justify-content: center; gap: 20px; margin-top: 15px;">
|
| 1065 |
-
<span style="background: rgba(255,255,255,0.2); padding:
|
| 1066 |
-
<span style="background: rgba(255,255,255,0.2); padding:
|
| 1067 |
</div>
|
| 1068 |
</div>
|
| 1069 |
""")
|
|
@@ -1072,8 +1308,9 @@ def create_gradio_app():
|
|
| 1072 |
with gr.Tab("π― Caption Generator"):
|
| 1073 |
with gr.Row():
|
| 1074 |
# Left Column - Controls
|
| 1075 |
-
with gr.Column(scale=2):
|
| 1076 |
gr.Markdown("### πΌοΈ Upload Images")
|
|
|
|
| 1077 |
|
| 1078 |
images = gr.File(
|
| 1079 |
label="πΈ Upload Images (Max 3)",
|
|
@@ -1128,7 +1365,7 @@ def create_gradio_app():
|
|
| 1128 |
)
|
| 1129 |
|
| 1130 |
# Right Column - Results
|
| 1131 |
-
with gr.Column(scale=3):
|
| 1132 |
gr.Markdown("### π Generated Content")
|
| 1133 |
|
| 1134 |
output = gr.Textbox(
|
|
@@ -1138,11 +1375,25 @@ def create_gradio_app():
|
|
| 1138 |
show_copy_button=True,
|
| 1139 |
placeholder="Upload images and generate your Instagram content..."
|
| 1140 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1141 |
|
| 1142 |
# Multi-Language Tab
|
| 1143 |
with gr.Tab("π Multi-Language"):
|
| 1144 |
with gr.Row():
|
| 1145 |
-
with gr.Column():
|
| 1146 |
gr.Markdown("### π£οΈ Global Content Creation")
|
| 1147 |
gr.Markdown("*Powered by Hugging Face Translation Models*")
|
| 1148 |
|
|
@@ -1168,7 +1419,7 @@ def create_gradio_app():
|
|
| 1168 |
variant="primary"
|
| 1169 |
)
|
| 1170 |
|
| 1171 |
-
with gr.Column():
|
| 1172 |
multilingual_output = gr.Textbox(
|
| 1173 |
label="πΊοΈ Multi-Language Captions",
|
| 1174 |
lines=20,
|
|
@@ -1176,6 +1427,161 @@ def create_gradio_app():
|
|
| 1176 |
placeholder="Culturally adapted captions for global audiences..."
|
| 1177 |
)
|
| 1178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1179 |
# Event Handlers
|
| 1180 |
generate_btn.click(
|
| 1181 |
fn=generate_advanced_caption_interface,
|
|
@@ -1183,6 +1589,13 @@ def create_gradio_app():
|
|
| 1183 |
outputs=[output, base_caption_input]
|
| 1184 |
)
|
| 1185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1186 |
# Multi-language translation
|
| 1187 |
translate_btn.click(
|
| 1188 |
fn=translate_caption_interface,
|
|
|
|
| 18 |
import os
|
| 19 |
import base64
|
| 20 |
import json
|
|
|
|
|
|
|
| 21 |
from datetime import datetime, timedelta
|
| 22 |
from typing import List, Dict, Optional, Any
|
| 23 |
import io
|
| 24 |
import re
|
| 25 |
from dataclasses import dataclass
|
|
|
|
| 26 |
import functools
|
| 27 |
import gc
|
| 28 |
|
|
|
|
| 30 |
if not os.environ.get("HF_TOKEN"):
|
| 31 |
print("β οΈ HF_TOKEN not found - translation features will use fallback mode")
|
| 32 |
|
| 33 |
+
# SambaNova API key should be set in environment variables
|
|
|
|
| 34 |
|
| 35 |
# Core libraries
|
| 36 |
import gradio as gr
|
| 37 |
+
from PIL import Image
|
|
|
|
|
|
|
| 38 |
from textblob import TextBlob
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# OpenAI for SambaNova
|
| 41 |
import openai
|
|
|
|
| 784 |
|
| 785 |
return ''.join(caption_parts)
|
| 786 |
|
| 787 |
+
async def generate_mistral_variations(self, base_caption: str, count: int = 3) -> List[str]:
|
| 788 |
+
"""Generate caption variations using Meta-Llama-3.2-3B-Instruct"""
|
| 789 |
+
variations = []
|
| 790 |
+
|
| 791 |
+
# Check if SambaNova client is available
|
| 792 |
+
if not self.sambanova_client:
|
| 793 |
+
print("β οΈ SambaNova client not available for Llama variations, using fallback")
|
| 794 |
+
for i in range(count):
|
| 795 |
+
fallback_variation = self.create_simple_variation(base_caption, i+1)
|
| 796 |
+
variations.append(f"{fallback_variation}\n\n⨠Generated by Fallback Method (SambaNova unavailable)")
|
| 797 |
+
return variations
|
| 798 |
+
|
| 799 |
+
# Different variation approaches to ensure variety
|
| 800 |
+
variation_styles = [
|
| 801 |
+
"Make this caption more energetic and exciting with different emojis",
|
| 802 |
+
"Create a more casual and friendly version of this caption",
|
| 803 |
+
"Write a shorter, punchier version that's more direct"
|
| 804 |
+
]
|
| 805 |
+
|
| 806 |
+
for i in range(count):
|
| 807 |
+
try:
|
| 808 |
+
# Create specific variation prompt for each attempt
|
| 809 |
+
variation_prompt = f"""You are creating Instagram caption variations. {variation_styles[i]}.
|
| 810 |
+
|
| 811 |
+
Original caption:
|
| 812 |
+
{base_caption}
|
| 813 |
+
|
| 814 |
+
Instructions:
|
| 815 |
+
- Keep the same hashtags but rearrange them
|
| 816 |
+
- Change the wording while keeping the same message
|
| 817 |
+
- Use different emojis and expressions
|
| 818 |
+
- Don't explain your changes
|
| 819 |
+
- Only output the new caption, nothing else
|
| 820 |
+
|
| 821 |
+
New caption:"""
|
| 822 |
+
|
| 823 |
+
print(f"π Generating Llama-3.2 variation {i+1}...")
|
| 824 |
+
|
| 825 |
+
# Generate variation using Meta-Llama-3.2-3B-Instruct model
|
| 826 |
+
response = self.sambanova_client.chat.completions.create(
|
| 827 |
+
model=self.variation_model, # Meta-Llama-3.2-3B-Instruct
|
| 828 |
+
messages=[
|
| 829 |
+
{"role": "system", "content": "You are a helpful Instagram caption writer. Only output the caption, no explanations."},
|
| 830 |
+
{"role": "user", "content": variation_prompt}
|
| 831 |
+
],
|
| 832 |
+
temperature=0.9, # Higher temperature for more variety
|
| 833 |
+
top_p=0.95,
|
| 834 |
+
max_tokens=200 # Limit output length
|
| 835 |
+
)
|
| 836 |
+
|
| 837 |
+
if response and response.choices and len(response.choices) > 0:
|
| 838 |
+
result = response.choices[0].message.content.strip()
|
| 839 |
+
|
| 840 |
+
# Clean up the result - remove explanatory text
|
| 841 |
+
lines = result.split('\n')
|
| 842 |
+
clean_result = []
|
| 843 |
+
|
| 844 |
+
for line in lines:
|
| 845 |
+
line = line.strip()
|
| 846 |
+
# Skip lines that look like explanations
|
| 847 |
+
if (line and
|
| 848 |
+
not line.startswith('This variation') and
|
| 849 |
+
not line.startswith('- ') and
|
| 850 |
+
not line.startswith('The ') and
|
| 851 |
+
not 'maintains the same' in line.lower() and
|
| 852 |
+
not 'variation' in line.lower()):
|
| 853 |
+
clean_result.append(line)
|
| 854 |
+
|
| 855 |
+
final_result = '\n'.join(clean_result).strip()
|
| 856 |
+
|
| 857 |
+
if final_result and len(final_result.strip()) > 20:
|
| 858 |
+
variations.append(f"{final_result}\n\n⨠Generated by Meta-Llama-3.2-3B-Instruct")
|
| 859 |
+
print(f"β
Llama-3.2 variation {i+1} generated successfully")
|
| 860 |
+
else:
|
| 861 |
+
print(f"β οΈ Poor response from Llama-3.2 for variation {i+1}, using fallback")
|
| 862 |
+
fallback_variation = self.create_simple_variation(base_caption, i+1)
|
| 863 |
+
variations.append(f"{fallback_variation}\n\n⨠Generated by Fallback Method (Poor response)")
|
| 864 |
+
else:
|
| 865 |
+
print(f"β οΈ Empty response from Llama-3.2 for variation {i+1}, using fallback")
|
| 866 |
+
fallback_variation = self.create_simple_variation(base_caption, i+1)
|
| 867 |
+
variations.append(f"{fallback_variation}\n\n⨠Generated by Fallback Method (Empty response)")
|
| 868 |
+
|
| 869 |
+
except Exception as e:
|
| 870 |
+
print(f"β οΈ Error generating Llama-3.2 variation {i+1}: {e}")
|
| 871 |
+
# Create a simple variation as fallback
|
| 872 |
+
fallback_variation = self.create_simple_variation(base_caption, i+1)
|
| 873 |
+
variations.append(f"{fallback_variation}\n\n⨠Generated by Fallback Method (Error: {str(e)[:50]})")
|
| 874 |
+
|
| 875 |
+
return variations
|
| 876 |
+
|
| 877 |
+
def create_simple_variation(self, base_caption: str, variation_num: int) -> str:
|
| 878 |
+
"""Create a simple variation when Mistral fails"""
|
| 879 |
+
|
| 880 |
+
# More sophisticated text modifications for fallback
|
| 881 |
+
if variation_num == 1:
|
| 882 |
+
# Variation 1: Change sentence starters and expressions
|
| 883 |
+
modified = base_caption.replace("This is", "Here's").replace("Check out", "Take a look at")
|
| 884 |
+
modified = modified.replace("Amazing", "Incredible").replace("Love", "Absolutely adore")
|
| 885 |
+
modified = modified.replace("Can't wait", "So excited").replace("Let me know", "Tell me")
|
| 886 |
+
return modified
|
| 887 |
+
|
| 888 |
+
elif variation_num == 2:
|
| 889 |
+
# Variation 2: Add emphasis and change punctuation
|
| 890 |
+
modified = base_caption.replace("!", " β¨").replace(".", "! ")
|
| 891 |
+
modified = modified.replace("Great", "Fantastic").replace("Beautiful", "Stunning")
|
| 892 |
+
modified = modified.replace("Perfect", "Absolutely perfect").replace("Share", "Don't forget to share")
|
| 893 |
+
return modified
|
| 894 |
+
|
| 895 |
+
else: # variation_num == 3
|
| 896 |
+
# Variation 3: Rearrange and substitute words
|
| 897 |
+
modified = base_caption.replace("π₯", "π«").replace("Follow", "Join me")
|
| 898 |
+
modified = modified.replace("Comment", "Drop a comment").replace("Tag", "Make sure to tag")
|
| 899 |
+
modified = modified.replace("What do you think", "I'd love to hear your thoughts")
|
| 900 |
+
return modified
|
| 901 |
+
|
| 902 |
async def generate_advanced_caption(self, images: List[Image.Image], style: str,
|
| 903 |
audience: str, custom_prompt: str = "") -> str:
|
| 904 |
"""Generate advanced caption with SambaNova integration"""
|
|
|
|
| 1052 |
return result, caption_only
|
| 1053 |
|
| 1054 |
|
| 1055 |
+
async def generate_multiple_captions_interface(uploaded_files, style, audience,
|
| 1056 |
+
custom_prompt):
|
| 1057 |
+
"""Generate multiple caption variations using Meta-Llama-3.2-3B-Instruct"""
|
| 1058 |
+
if not setup_success:
|
| 1059 |
+
return f"β Setup Error: {setup_error}"
|
| 1060 |
+
|
| 1061 |
+
images = []
|
| 1062 |
+
if uploaded_files:
|
| 1063 |
+
for file in uploaded_files[:3]:
|
| 1064 |
+
try:
|
| 1065 |
+
image = Image.open(file.name)
|
| 1066 |
+
images.append(image)
|
| 1067 |
+
except Exception as e:
|
| 1068 |
+
return f"β Error processing file: {e}"
|
| 1069 |
+
|
| 1070 |
+
if not images:
|
| 1071 |
+
return "β Please upload at least one image to generate caption variations."
|
| 1072 |
+
|
| 1073 |
+
# First generate the main caption using Llama-4-Maverick
|
| 1074 |
+
main_result = await generator.generate_advanced_caption(
|
| 1075 |
+
images, style, audience, custom_prompt
|
| 1076 |
+
)
|
| 1077 |
+
|
| 1078 |
+
# Extract just the caption text (without the header and footer)
|
| 1079 |
+
base_caption = ""
|
| 1080 |
+
if "β¨ AI-GENERATED INSTAGRAM CONTENT:" in main_result:
|
| 1081 |
+
lines = main_result.split('\n')
|
| 1082 |
+
caption_lines = []
|
| 1083 |
+
start_capturing = False
|
| 1084 |
+
|
| 1085 |
+
for line in lines:
|
| 1086 |
+
if "β¨ AI-GENERATED INSTAGRAM CONTENT:" in line:
|
| 1087 |
+
start_capturing = True
|
| 1088 |
+
continue
|
| 1089 |
+
elif "π€ Powered by SambaNova" in line:
|
| 1090 |
+
break
|
| 1091 |
+
elif start_capturing and line.strip():
|
| 1092 |
+
caption_lines.append(line)
|
| 1093 |
+
|
| 1094 |
+
base_caption = '\n'.join(caption_lines).strip()
|
| 1095 |
+
|
| 1096 |
+
if not base_caption:
|
| 1097 |
+
return "β Failed to generate base caption for variations"
|
| 1098 |
+
|
| 1099 |
+
# Generate 3 variations using Meta-Llama-3.2-3B-Instruct
|
| 1100 |
+
variations = await generator.generate_mistral_variations(base_caption, count=3)
|
| 1101 |
+
|
| 1102 |
+
# Format the results
|
| 1103 |
+
formatted_result = "β¨ ALTERNATIVE CAPTIONS:\n\n"
|
| 1104 |
+
formatted_result += "=" * 60 + "\n\n"
|
| 1105 |
+
|
| 1106 |
+
for i, variation in enumerate(variations, 1):
|
| 1107 |
+
formatted_result += f"π ALTERNATIVE {i}:\n"
|
| 1108 |
+
formatted_result += f"{variation}\n"
|
| 1109 |
+
formatted_result += "=" * 60 + "\n\n"
|
| 1110 |
+
|
| 1111 |
+
return formatted_result
|
| 1112 |
+
|
| 1113 |
+
|
| 1114 |
async def translate_caption_interface(base_caption, selected_languages):
|
| 1115 |
"""Generate multi-language versions of captions"""
|
| 1116 |
if not base_caption.strip():
|
|
|
|
| 1210 |
|
| 1211 |
|
| 1212 |
def create_gradio_app():
|
| 1213 |
+
"""Create the Gradio app with good UI styling"""
|
| 1214 |
|
| 1215 |
# Status indicators
|
| 1216 |
hf_status = "β
Connected" if generator and generator.hf_client_working else "β οΈ Fallback Mode"
|
| 1217 |
sambanova_status = "β
Connected" if generator and generator.sambanova_client_working else "β οΈ Fallback Mode"
|
| 1218 |
|
| 1219 |
+
# Enhanced CSS for better UI
|
| 1220 |
+
css = """
|
| 1221 |
+
@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300;400;500;600;700&display=swap');
|
| 1222 |
+
|
| 1223 |
+
.gradio-container {
|
| 1224 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #f5576c 75%, #4facfe 100%);
|
| 1225 |
+
font-family: 'Space Grotesk', 'Inter', system-ui, sans-serif;
|
| 1226 |
+
min-height: 100vh;
|
| 1227 |
+
}
|
| 1228 |
+
|
| 1229 |
+
.main-header {
|
| 1230 |
+
text-align: center;
|
| 1231 |
+
color: white;
|
| 1232 |
+
margin-bottom: 30px;
|
| 1233 |
+
padding: 30px;
|
| 1234 |
+
background: rgba(255,255,255,0.1);
|
| 1235 |
+
border-radius: 20px;
|
| 1236 |
+
backdrop-filter: blur(20px);
|
| 1237 |
+
border: 1px solid rgba(255,255,255,0.2);
|
| 1238 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
| 1239 |
+
}
|
| 1240 |
+
|
| 1241 |
+
.feature-card {
|
| 1242 |
+
background: rgba(255,255,255,0.1);
|
| 1243 |
+
border-radius: 15px;
|
| 1244 |
+
padding: 20px;
|
| 1245 |
+
backdrop-filter: blur(15px);
|
| 1246 |
+
border: 1px solid rgba(255,255,255,0.2);
|
| 1247 |
+
box-shadow: 0 15px 30px rgba(0,0,0,0.1);
|
| 1248 |
+
margin: 10px 0;
|
| 1249 |
+
}
|
| 1250 |
+
|
| 1251 |
+
.gradio-button-primary {
|
| 1252 |
+
background: linear-gradient(45deg, #ff6b6b, #ee5a24, #ff9ff3, #54a0ff) !important;
|
| 1253 |
+
border: none !important;
|
| 1254 |
+
border-radius: 15px !important;
|
| 1255 |
+
padding: 15px 25px !important;
|
| 1256 |
+
font-weight: 600 !important;
|
| 1257 |
+
color: white !important;
|
| 1258 |
+
box-shadow: 0 10px 20px rgba(255, 107, 107, 0.3) !important;
|
| 1259 |
+
transition: all 0.3s ease !important;
|
| 1260 |
+
}
|
| 1261 |
+
|
| 1262 |
+
.gradio-button-primary:hover {
|
| 1263 |
+
transform: translateY(-2px) !important;
|
| 1264 |
+
box-shadow: 0 15px 30px rgba(255, 107, 107, 0.5) !important;
|
| 1265 |
+
}
|
| 1266 |
+
|
| 1267 |
+
.gradio-button-secondary {
|
| 1268 |
+
background: linear-gradient(45deg, #feca57, #ff9ff3, #54a0ff, #5f27cd) !important;
|
| 1269 |
+
border: none !important;
|
| 1270 |
+
border-radius: 12px !important;
|
| 1271 |
+
padding: 12px 20px !important;
|
| 1272 |
+
font-weight: 600 !important;
|
| 1273 |
+
color: white !important;
|
| 1274 |
+
box-shadow: 0 8px 16px rgba(254, 202, 87, 0.3) !important;
|
| 1275 |
+
}
|
| 1276 |
+
|
| 1277 |
+
.status-badge {
|
| 1278 |
+
background: linear-gradient(90deg, #2ecc71, #27ae60);
|
| 1279 |
+
color: white;
|
| 1280 |
+
padding: 10px 20px;
|
| 1281 |
+
border-radius: 10px;
|
| 1282 |
+
text-align: center;
|
| 1283 |
+
font-weight: 600;
|
| 1284 |
+
box-shadow: 0 8px 16px rgba(46, 204, 113, 0.3);
|
| 1285 |
+
margin: 10px 0;
|
| 1286 |
+
}
|
| 1287 |
+
"""
|
| 1288 |
+
|
| 1289 |
+
with gr.Blocks(css=css, title="π± Instagram Generator", theme=gr.themes.Glass()) as app:
|
| 1290 |
|
| 1291 |
# Main Header
|
| 1292 |
gr.HTML(f"""
|
| 1293 |
+
<div class="main-header">
|
| 1294 |
+
<h1 style="font-size: 2.5rem; margin-bottom: 15px; font-weight: 800; background: linear-gradient(45deg, #ff6b6b, #feca57, #ff9ff3, #54a0ff); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
|
| 1295 |
π± INSTAGRAM CAPTION GENERATOR
|
| 1296 |
</h1>
|
| 1297 |
<h2 style="font-size: 1.2rem; margin-bottom: 20px; opacity: 0.9;">
|
| 1298 |
π AI-Powered Content Creation β’ SambaNova + Hugging Face
|
| 1299 |
</h2>
|
| 1300 |
<div style="display: flex; justify-content: center; gap: 20px; margin-top: 15px;">
|
| 1301 |
+
<span style="background: rgba(255,255,255,0.2); padding: 8px 16px; border-radius: 15px;">π€ SambaNova: {sambanova_status}</span>
|
| 1302 |
+
<span style="background: rgba(255,255,255,0.2); padding: 8px 16px; border-radius: 15px;">π€ Hugging Face: {hf_status}</span>
|
| 1303 |
</div>
|
| 1304 |
</div>
|
| 1305 |
""")
|
|
|
|
| 1308 |
with gr.Tab("π― Caption Generator"):
|
| 1309 |
with gr.Row():
|
| 1310 |
# Left Column - Controls
|
| 1311 |
+
with gr.Column(scale=2, elem_classes=["feature-card"]):
|
| 1312 |
gr.Markdown("### πΌοΈ Upload Images")
|
| 1313 |
+
gr.Markdown("*SambaNova AI vision analysis with quality scoring*")
|
| 1314 |
|
| 1315 |
images = gr.File(
|
| 1316 |
label="πΈ Upload Images (Max 3)",
|
|
|
|
| 1365 |
)
|
| 1366 |
|
| 1367 |
# Right Column - Results
|
| 1368 |
+
with gr.Column(scale=3, elem_classes=["feature-card"]):
|
| 1369 |
gr.Markdown("### π Generated Content")
|
| 1370 |
|
| 1371 |
output = gr.Textbox(
|
|
|
|
| 1375 |
show_copy_button=True,
|
| 1376 |
placeholder="Upload images and generate your Instagram content..."
|
| 1377 |
)
|
| 1378 |
+
|
| 1379 |
+
with gr.Row():
|
| 1380 |
+
alternatives_btn = gr.Button(
|
| 1381 |
+
"β¨ Generate 3 Alternative Captions",
|
| 1382 |
+
variant="secondary",
|
| 1383 |
+
scale=1
|
| 1384 |
+
)
|
| 1385 |
+
|
| 1386 |
+
alternatives_output = gr.Textbox(
|
| 1387 |
+
label="β¨ Alternative Captions",
|
| 1388 |
+
lines=15,
|
| 1389 |
+
show_copy_button=True,
|
| 1390 |
+
placeholder="Generate 3 different caption alternatives using Meta-Llama-3.2-3B-Instruct..."
|
| 1391 |
+
)
|
| 1392 |
|
| 1393 |
# Multi-Language Tab
|
| 1394 |
with gr.Tab("π Multi-Language"):
|
| 1395 |
with gr.Row():
|
| 1396 |
+
with gr.Column(elem_classes=["feature-card"]):
|
| 1397 |
gr.Markdown("### π£οΈ Global Content Creation")
|
| 1398 |
gr.Markdown("*Powered by Hugging Face Translation Models*")
|
| 1399 |
|
|
|
|
| 1419 |
variant="primary"
|
| 1420 |
)
|
| 1421 |
|
| 1422 |
+
with gr.Column(elem_classes=["feature-card"]):
|
| 1423 |
multilingual_output = gr.Textbox(
|
| 1424 |
label="πΊοΈ Multi-Language Captions",
|
| 1425 |
lines=20,
|
|
|
|
| 1427 |
placeholder="Culturally adapted captions for global audiences..."
|
| 1428 |
)
|
| 1429 |
|
| 1430 |
+
# SambaNova Features Tab
|
| 1431 |
+
with gr.Tab("π€ SambaNova Features"):
|
| 1432 |
+
gr.HTML(f"""
|
| 1433 |
+
<div class="status-badge">
|
| 1434 |
+
π SambaNova Status: {sambanova_status} β’ π¦ Llama-4-Maverick + Llama-3.2-3B
|
| 1435 |
+
</div>
|
| 1436 |
+
""")
|
| 1437 |
+
|
| 1438 |
+
with gr.Row():
|
| 1439 |
+
with gr.Column(elem_classes=["feature-card"]):
|
| 1440 |
+
gr.HTML("""
|
| 1441 |
+
<div style="text-align: center; padding: 20px;">
|
| 1442 |
+
<h3>β‘ SambaNova AI Capabilities</h3>
|
| 1443 |
+
<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 15px; margin-top: 20px;">
|
| 1444 |
+
<div style="padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px;">
|
| 1445 |
+
<h4 style="color: #2ecc71; margin: 0;">Llama-4-Maverick</h4>
|
| 1446 |
+
<p style="margin: 5px 0; opacity: 0.8;">Main Caption Generation</p>
|
| 1447 |
+
</div>
|
| 1448 |
+
<div style="padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px;">
|
| 1449 |
+
<h4 style="color: #3498db; margin: 0;">Llama-3.2-3B</h4>
|
| 1450 |
+
<p style="margin: 5px 0; opacity: 0.8;">Caption Variations</p>
|
| 1451 |
+
</div>
|
| 1452 |
+
<div style="padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px;">
|
| 1453 |
+
<h4 style="color: #e74c3c; margin: 0;">Multi-Modal</h4>
|
| 1454 |
+
<p style="margin: 5px 0; opacity: 0.8;">Vision + Text Analysis</p>
|
| 1455 |
+
</div>
|
| 1456 |
+
<div style="padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px;">
|
| 1457 |
+
<h4 style="color: #f39c12; margin: 0;">Advanced</h4>
|
| 1458 |
+
<p style="margin: 5px 0; opacity: 0.8;">Style & Audience Targeting</p>
|
| 1459 |
+
</div>
|
| 1460 |
+
</div>
|
| 1461 |
+
</div>
|
| 1462 |
+
""")
|
| 1463 |
+
|
| 1464 |
+
with gr.Column(elem_classes=["feature-card"]):
|
| 1465 |
+
gr.Code(
|
| 1466 |
+
value="""
|
| 1467 |
+
# SambaNova API Integration:
|
| 1468 |
+
|
| 1469 |
+
from openai import OpenAI
|
| 1470 |
+
|
| 1471 |
+
client = OpenAI(
|
| 1472 |
+
api_key=os.environ["SAMBANOVA_API_KEY"],
|
| 1473 |
+
base_url="https://api.sambanova.ai/v1",
|
| 1474 |
+
)
|
| 1475 |
+
|
| 1476 |
+
# Main caption generation
|
| 1477 |
+
response = client.chat.completions.create(
|
| 1478 |
+
model="Llama-4-Maverick-17B-128E-Instruct",
|
| 1479 |
+
messages=[{
|
| 1480 |
+
"role": "user",
|
| 1481 |
+
"content": [
|
| 1482 |
+
{"type": "text", "text": "Create Instagram caption"},
|
| 1483 |
+
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,..."}}
|
| 1484 |
+
]
|
| 1485 |
+
}],
|
| 1486 |
+
temperature=0.1,
|
| 1487 |
+
top_p=0.1
|
| 1488 |
+
)
|
| 1489 |
+
|
| 1490 |
+
# Caption variations
|
| 1491 |
+
variations = client.chat.completions.create(
|
| 1492 |
+
model="Meta-Llama-3.2-3B-Instruct",
|
| 1493 |
+
messages=[{
|
| 1494 |
+
"role": "user",
|
| 1495 |
+
"content": "Create different version of this caption..."
|
| 1496 |
+
}],
|
| 1497 |
+
temperature=0.9,
|
| 1498 |
+
top_p=0.95
|
| 1499 |
+
)
|
| 1500 |
+
""",
|
| 1501 |
+
language="python",
|
| 1502 |
+
label="π§ SambaNova Integration Code"
|
| 1503 |
+
)
|
| 1504 |
+
|
| 1505 |
+
# Hugging Face Features Tab
|
| 1506 |
+
with gr.Tab("π€ Hugging Face Features"):
|
| 1507 |
+
gr.HTML(f"""
|
| 1508 |
+
<div class="status-badge">
|
| 1509 |
+
π€ Hugging Face Status: {hf_status} β’ π Multi-Language Translation
|
| 1510 |
+
</div>
|
| 1511 |
+
""")
|
| 1512 |
+
|
| 1513 |
+
with gr.Row():
|
| 1514 |
+
with gr.Column(elem_classes=["feature-card"]):
|
| 1515 |
+
gr.HTML("""
|
| 1516 |
+
<div style="text-align: center; padding: 20px;">
|
| 1517 |
+
<h3>π Translation Models</h3>
|
| 1518 |
+
<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 15px; margin-top: 20px;">
|
| 1519 |
+
<div style="padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px;">
|
| 1520 |
+
<h4 style="color: #2ecc71; margin: 0;">π©πͺ German</h4>
|
| 1521 |
+
<p style="margin: 5px 0; opacity: 0.8;">google-t5/t5-small</p>
|
| 1522 |
+
</div>
|
| 1523 |
+
<div style="padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px;">
|
| 1524 |
+
<h4 style="color: #3498db; margin: 0;">π¨π³ Chinese</h4>
|
| 1525 |
+
<p style="margin: 5px 0; opacity: 0.8;">chence08/mt5-small-iwslt2017-zh-en</p>
|
| 1526 |
+
</div>
|
| 1527 |
+
<div style="padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px;">
|
| 1528 |
+
<h4 style="color: #e74c3c; margin: 0;">π«π· French</h4>
|
| 1529 |
+
<p style="margin: 5px 0; opacity: 0.8;">google-t5/t5-large</p>
|
| 1530 |
+
</div>
|
| 1531 |
+
<div style="padding: 15px; background: rgba(255,255,255,0.1); border-radius: 10px;">
|
| 1532 |
+
<h4 style="color: #f39c12; margin: 0;">πΈπ¦ Arabic</h4>
|
| 1533 |
+
<p style="margin: 5px 0; opacity: 0.8;">marefa-nlp/marefa-mt-en-ar</p>
|
| 1534 |
+
</div>
|
| 1535 |
+
</div>
|
| 1536 |
+
</div>
|
| 1537 |
+
""")
|
| 1538 |
+
|
| 1539 |
+
with gr.Column(elem_classes=["feature-card"]):
|
| 1540 |
+
gr.Code(
|
| 1541 |
+
value="""
|
| 1542 |
+
# Hugging Face Translation Integration:
|
| 1543 |
+
|
| 1544 |
+
from huggingface_hub import InferenceClient
|
| 1545 |
+
|
| 1546 |
+
client = InferenceClient(
|
| 1547 |
+
provider="hf-inference",
|
| 1548 |
+
api_key=os.environ["HF_TOKEN"],
|
| 1549 |
+
)
|
| 1550 |
+
|
| 1551 |
+
# German translation
|
| 1552 |
+
german_result = client.translation(
|
| 1553 |
+
"This is an amazing moment! β¨",
|
| 1554 |
+
model="google-t5/t5-small",
|
| 1555 |
+
)
|
| 1556 |
+
|
| 1557 |
+
# Chinese translation
|
| 1558 |
+
chinese_result = client.translation(
|
| 1559 |
+
"Amazing content for Instagram",
|
| 1560 |
+
model="chence08/mt5-small-iwslt2017-zh-en",
|
| 1561 |
+
)
|
| 1562 |
+
|
| 1563 |
+
# French translation
|
| 1564 |
+
french_result = client.translation(
|
| 1565 |
+
"Beautiful content creation",
|
| 1566 |
+
model="google-t5/t5-large",
|
| 1567 |
+
)
|
| 1568 |
+
|
| 1569 |
+
# Arabic translation
|
| 1570 |
+
arabic_result = client.translation(
|
| 1571 |
+
"Social media content",
|
| 1572 |
+
model="marefa-nlp/marefa-mt-en-ar",
|
| 1573 |
+
)
|
| 1574 |
+
|
| 1575 |
+
# Features:
|
| 1576 |
+
# β
4 language models
|
| 1577 |
+
# β
Fallback support
|
| 1578 |
+
# β
Instagram-optimized output
|
| 1579 |
+
# β
Cultural adaptation
|
| 1580 |
+
""",
|
| 1581 |
+
language="python",
|
| 1582 |
+
label="π§ Hugging Face Translation Code"
|
| 1583 |
+
)
|
| 1584 |
+
|
| 1585 |
# Event Handlers
|
| 1586 |
generate_btn.click(
|
| 1587 |
fn=generate_advanced_caption_interface,
|
|
|
|
| 1589 |
outputs=[output, base_caption_input]
|
| 1590 |
)
|
| 1591 |
|
| 1592 |
+
# Generate multiple alternatives
|
| 1593 |
+
alternatives_btn.click(
|
| 1594 |
+
fn=generate_multiple_captions_interface,
|
| 1595 |
+
inputs=[images, caption_style, target_audience, custom_prompt],
|
| 1596 |
+
outputs=alternatives_output
|
| 1597 |
+
)
|
| 1598 |
+
|
| 1599 |
# Multi-language translation
|
| 1600 |
translate_btn.click(
|
| 1601 |
fn=translate_caption_interface,
|