import gradio as gr import anthropic import openai from openai import OpenAI import base64 from io import BytesIO from PIL import Image import os from dotenv import load_dotenv from deepgram import DeepgramClient import soundfile as sf import tempfile # Load environment variables load_dotenv() ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY') OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') DEEPGRAM_API_KEY = "441813237a6b8b9a1306f48c293b35399b7101c9" if not ANTHROPIC_API_KEY or not OPENAI_API_KEY: raise ValueError("API keys not found in environment variables") # Initialize clients claude_client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY) openai_client = OpenAI( api_key=OPENAI_API_KEY, base_url="https://api.model.box/v1" ) deepgram_client = DeepgramClient(DEEPGRAM_API_KEY) def encode_image_to_base64(image): buffered = BytesIO() image.save(buffered, format="PNG") return base64.b64encode(buffered.getvalue()).decode('utf-8') def analyze_image(image): try: if image is None: return "Error: Please upload an image" # Ensure image is in RGB mode if len(image.shape) == 3 and image.shape[2] == 4: img = Image.fromarray(image).convert('RGB') else: img = Image.fromarray(image) base64_image = encode_image_to_base64(img) prompt = """Analyze this greeting card image in detail: 1. Describe the main visual elements and composition 2. Identify the likely target audience 3. Analyze the style, mood, and atmosphere 4. List the key design elements and techniques 5. Explain how text and image work together 6. Suggest potential variations or improvements""" message = claude_client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1000, temperature=0, messages=[{ "role": "user", "content": [ { "type": "image", "source": { "type": "base64", "media_type": "image/png", "data": base64_image } }, { "type": "text", "text": prompt } ] }] ) return message.content[0].text except Exception as e: return f"Error: {str(e)}" def generate_prompt(target_audience, style_mood, color_scheme, main_motif, occasion, reference_analysis=None): try: base_prompt = f"""Create a detailed prompt for FreePik AI to generate an image based on these requirements: Target Audience: {target_audience} Style/Mood: {style_mood} Color Scheme: {color_scheme} Main Motif: {main_motif} Occasion: {occasion}""" if reference_analysis and reference_analysis.strip(): base_prompt += f""" Reference Analysis for Style Inspiration: {reference_analysis}""" prompt = base_prompt + """ Please structure the prompt in this format: 1. Main description (avoid using words like 'card', 'greeting card', or 'postcard' as we don't want the image to contain a card itself) 2. Style and composition details 3. Color and lighting specifications 4. Technical parameters 5. Additional details and refinements Important: Do not mention 'card', 'greeting card', or 'postcard' in the prompt, as we want to create the artwork itself, not an image of a card. Focus on describing the visual elements, composition, and style directly. Also add this negative prompt at the end: NEGATIVE PROMPT: greeting card, postcard, card border, card template, paper card, folded paper, card design, card layout""" response = openai_client.chat.completions.create( model="anthropic/claude-3-5-sonnet-20241022", messages=[ { "role": "user", "content": prompt } ] ) return response.choices[0].message.content except Exception as e: return f"Error: {str(e)}" def process_audio(audio): try: if audio is None: return "No audio detected", "Please record some audio first" # Create a temporary WAV file with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio: # audio from Gradio comes as tuple of (sample_rate, audio_data) sample_rate, audio_data = audio # Save the audio data to the temporary file sf.write(temp_audio.name, audio_data, sample_rate) # Open and read the temporary file with open(temp_audio.name, 'rb') as audio_file: response = deepgram_client.listen.rest.v('1').transcribe_file( {'buffer': audio_file.read(), 'mimetype': 'audio/wav'}, { 'smart_format': True, 'model': 'whisper-large', 'language': 'de' } ) # Clean up the temporary file os.unlink(temp_audio.name) # Extract the transcribed text transcribed_text = response.results.channels[0].alternatives[0].transcript # Create a prompt for Claude/OpenAI to interpret the transcribed text interpretation_prompt = f""" Analyze the following German text and extract information about a greeting card design. If certain aspects are not mentioned, use creative interpretation based on what IS mentioned. Do not use default values - interpret the context and intent of what was said. Text to analyze: {transcribed_text} Please provide the following information in exactly this format: Target audience: [interpreted target audience] Style/Mood: [interpreted style/mood] Color scheme: [interpreted color scheme] Main motif: [interpreted main motif] Occasion: [interpreted occasion] """ # Get interpretation from OpenAI interpretation = openai_client.chat.completions.create( model="anthropic/claude-3-5-sonnet-20241022", messages=[ { "role": "user", "content": interpretation_prompt } ] ) # Get the interpreted text interpreted_text = interpretation.choices[0].message.content # Parse the interpretation to extract the lines lines = interpreted_text.split('\n') params = {} for line in lines: if ':' in line: key, value = line.split(':', 1) key = key.strip().lower().replace('/', '_') params[key] = value.strip() # Use the interpreted text to generate the final prompt result = generate_prompt( target_audience=params.get('target audience', ''), style_mood=params.get('style mood', ''), color_scheme=params.get('color scheme', ''), main_motif=params.get('main motif', ''), occasion=params.get('occasion', ''), reference_analysis=params.get('reference analysis', '') ) return transcribed_text, result except Exception as e: return f"Error: {str(e)}", "Fehler bei der Verarbeitung" # Create the Gradio interface with gr.Blocks( title="Hartung Workshop Prompter", theme=gr.themes.Soft(), css=""" .gradio-container { max-width: 950px; margin: auto; font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif; } /* Import Inter font */ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap'); /* Apply font to all text elements */ * { font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important; } /* Make headings more prominent */ h1, h2, h3 { font-weight: 600 !important; } /* Improve readability of regular text */ p, label, span, textarea { font-weight: 400; line-height: 1.5; } """ ) as app: gr.Markdown(""" # Hartung Workshop Prompter *Ein Tool zur Generierung und Analyse von Bildprompts* """) with gr.Tabs(selected=0): # selected=0 ensures first tab is active with gr.Tab("🔍 Bildanalyse"): with gr.Row(): image_input = gr.Image(label="Karte hochladen", scale=2) # scale=2 makes it wider with gr.Column(scale=1): # scale=1 makes it narrower analysis_output = gr.Textbox( label="Analyseergebnis", lines=12, # Increased lines for better readability show_copy_button=True ) analyze_btn = gr.Button("Bild analysieren", variant="primary") # Make button more prominent # Add this line to connect the button to the function analyze_btn.click( fn=analyze_image, inputs=image_input, outputs=analysis_output ) with gr.Tab("✨ Prompt-Generierung"): with gr.Row(): with gr.Column(): target_audience = gr.Textbox(label="Zielgruppe", placeholder="z.B. Frauen 30-50, naturinteressiert") style_mood = gr.Textbox(label="Stil/Stimmung", placeholder="z.B. Weich, Aquarell, romantisch") color_scheme = gr.Textbox(label="Farbschema", placeholder="z.B. Pastellblau und rosa") main_motif = gr.Textbox(label="Hauptmotiv", placeholder="z.B. Frühlingsblumen mit Schmetterlingen") occasion = gr.Textbox(label="Anlass", placeholder="z.B. Geburtstag, Hochzeit, Ostern") with gr.Column(): reference_analysis = gr.Textbox( label="Referenzanalyse (Optional)", placeholder="Fügen Sie hier die Analyse eines Referenzbildes ein, um ähnliche Stilelemente zu übernehmen", lines=10 ) prompt_output = gr.Textbox( label="Generierter Prompt", lines=10, show_copy_button=True ) generate_btn = gr.Button("Prompt generieren") generate_btn.click( generate_prompt, inputs=[target_audience, style_mood, color_scheme, main_motif, occasion, reference_analysis], outputs=prompt_output ) with gr.Tab("Spracheingabe"): gr.Markdown(""" # Sprach-zu-Prompt Generator Sprechen Sie Ihre Bildidee ein. Beschreiben Sie: - Zielgruppe - Stil/Stimmung - Farbschema - Hauptmotiv - Anlass """) audio_input = gr.Audio( sources=["microphone"], type="numpy", label="Mikrofon", streaming=False ) with gr.Row(): transcription_output = gr.Textbox( label="Transkription", lines=3, show_copy_button=True ) voice_prompt_output = gr.Textbox( label="Generierter Prompt", lines=10, show_copy_button=True ) audio_input.change( fn=process_audio, inputs=[audio_input], outputs=[transcription_output, voice_prompt_output] ) with gr.Tab("Übungen & Tutorials"): gr.Markdown(""" # 🎓 Prompt Engineering Workshop ## Lektion 1: Grundlagen des Prompt Engineerings ### 🎯 Lernziele - Verstehen der Grundstruktur eines effektiven Prompts - Kennenlernen der Do's und Don'ts - Praktische Übungen zur Prompt-Optimierung ### 📝 Beispiel: Basis-Prompt vs. Optimierter Prompt #### Basis-Prompt: ``` Erstelle ein Bild mit Blumen ``` #### Optimierter Prompt: ``` Erstelle eine detaillierte Komposition von Wildblumen in Pastelltönen, mit sanftem Morgenlicht, weichen Schatten und einer atmosphärischen Tiefenwirkung. Stil: Aquarell-inspiriert Hauptfarben: Zartes Rosa, Lavendel, Hellblau Stimmung: Romantisch, verträumt ``` ### 🎯 Übung 1: Prompt-Verbesserung """) with gr.Group(): gr.Markdown(""" Verbessern Sie den folgenden Basic-Prompt: *"Mache eine Geburtstagskarte"* """) exercise_input = gr.Textbox( label="Ihr verbesserter Prompt", placeholder="Schreiben Sie hier Ihren optimierten Prompt...", lines=5 ) example_solution = gr.Markdown(visible=False, value=""" ### 💡 Beispiellösung: ``` Erstelle eine festliche Komposition mit folgenden Eigenschaften: - Hauptmotiv: Stilisierte Zahlen und luftige Konfetti-Elemente - Farbschema: Lebendiges Türkis mit Gold-Akzenten - Stil: Modern-minimalistisch mit verspielten Details - Komposition: Dynamische Diagonale mit Fokuspunkt - Stimmung: Heiter und einladend ``` """) show_solution_btn = gr.Button("Lösung anzeigen") show_solution_btn.click( lambda: gr.Markdown.update(visible=True), None, example_solution ) gr.Markdown(""" ### 📚 Weiterführende Ressourcen 1. **Best Practices für Bildgenerierung** - Spezifische Details vor vagen Beschreibungen - Klare Struktur der Prompt-Elemente - Verwendung von Stilreferenzen 2. **Häufige Fehler** - Zu vage Beschreibungen - Widersprüchliche Anweisungen - Fehlende Kontextinformationen ### 🎯 Nächste Übung folgt... """) with gr.Accordion("💡 Profi-Tipps"): gr.Markdown(""" - Beginnen Sie immer mit dem wichtigsten Element - Nutzen Sie präzise Adjektive - Denken Sie an die Zielgruppe - Testen Sie verschiedene Variationen """) with gr.Tab("📖 Prozess & Anleitung"): gr.Markdown(""" # 🎨 KI-gestützte Kartenerstellung: Der komplette Prozess ## 1️⃣ Konzeptionsphase ### Grundlegende Analyse - 🎯 Bestimmen Sie den Anlass der Karte - 👥 Definieren Sie die Zielgruppe - 💭 Überlegen Sie sich die gewünschte emotionale Wirkung - 🎨 Wählen Sie ein passendes Farbschema - 🖼️ Definieren Sie die Hauptmotive ### Dokumentation der Anforderungen - Anlass: [z.B. Geburtstag, Hochzeit, etc.] - Zielgruppe: [Alter, Interessen, etc.] - Emotionale Wirkung: [z.B. fröhlich, besinnlich, etc.] - Farbschema: [Hauptfarben und Akzente] - Hauptmotive: [zentrale Bildelemente] """) with gr.Accordion("🔍 2. Prompt-Erstellung"): gr.Markdown(""" ### Master-Prompt Template ``` Please help me create a detailed image generation prompt based on the following information: - Occasion: [Anlass] - Target group: [Zielgruppe] - Style preference: [Stil] - Color preferences: [Farben] - Main motif/elements: [Hauptmotive] - Mood/feeling: [Stimmung] - Text placement needs: [Textplatzierung] ``` """) with gr.Accordion("🎨 3. Bildgenerierung"): gr.Markdown(""" ### FreePik AI Einstellungen - Höchste Qualitätsstufe wählen - Auflösung: 1024x1024 - Mindestens 4 Variationen erstellen ### Qualitätskontrolle - Vergleich der Variationen - Auswahl der besten Version """) with gr.Accordion("⚖️ 4. Rechtliche Prüfung"): gr.Markdown(""" ### Google Reverse Image Search 1. Bild in Web-Auflösung exportieren 2. Auf images.google.com hochladen 3. Prüfen auf: - Ähnliche Kompositionen - Geschützte Designs - Markenelemente ### Dokumentation - Screenshot der Suchergebnisse - Datum der Überprüfung - Anpassungsdokumentation """) with gr.Accordion("✍️ 5. Texterstellung"): gr.Markdown(""" ### KI-Textgenerierung Prompt ``` Basierend auf folgendem Kartenkonzept, erstelle 3 Textvarianten: - Anlass: [X] - Zielgruppe: [X] - Stil: [X] - Stimmung: [X] Bitte erstelle: 1. Kurze, prägnante Version 2. Poetische Version 3. Moderne Version Plus: - Schriftart-Empfehlung - Größenempfehlung - Platzierungsvorschlag - Farbempfehlung ``` """) with gr.Accordion("🎯 6. Finale Checkliste"): gr.Markdown(""" - [ ] Konzept vollständig dokumentiert - [ ] Prompt optimiert und gespeichert - [ ] Bildrechte geprüft und dokumentiert - [ ] Text rechtschreibgeprüft - [ ] Qualitätskontrolle durchgeführt - [ ] Alle Dateien gesichert - [ ] Dokumentation vollständig """) # Launch the app if __name__ == "__main__": app.launch(share=True)