Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from groq import Groq | |
| from urllib.parse import quote | |
| def get_stock_url(site, phrase): | |
| if site == "getty": | |
| return f"https://www.gettyimages.com/search/2/image?family=editorial&phrase={quote(phrase)}" | |
| elif site == "adobe": | |
| return f"https://stock.adobe.com/search?k={quote(phrase)}" | |
| def groq_conceptual_keywords(api_key, text, top_k=10): | |
| if not api_key or not api_key.strip(): | |
| return "⚠️ Please enter your Groq API key." | |
| if not text or not text.strip(): | |
| return "⚠️ Please enter a description." | |
| client = Groq(api_key=api_key.strip()) | |
| PROMPT = ( | |
| "As an expert in visual storytelling for global editorial websites, generate a list of 10 conceptual and visually descriptive image search keywords, keep in mind that it doesnt have to be too long because we could get less images because of those extra words" | |
| "given the following description or topic. " | |
| "Each should be a potential image theme, activity, mood, or visual setting directly suitable for stock photo search— MAKE SURE ITS RELAVANT TO THE SCENE OR CONTEXT I PUT IN " | |
| "not literal repeats of the input, but creative, high-level, relevant concepts. " | |
| "List each concept/theme on a new line, no numbering or bullets. First try the most obivious according to the keyword chances are we could find the perfect fit instead of coneceptional\n\n" | |
| f"Description: {text}\n" | |
| "Conceptual Visual Keywords:\n" | |
| ) | |
| try: | |
| completion = client.chat.completions.create( | |
| messages=[{"role": "user", "content": PROMPT}], | |
| model="llama-3.3-70b-versatile", # Or use "llama-3-3-70b-versatile" | |
| temperature=0.85, | |
| max_tokens=160 | |
| ) | |
| content = completion.choices[0].message.content | |
| except Exception as e: | |
| return f"❌ Groq API error: {e}" | |
| # Extract all lines, clean, filter | |
| concepts = [ | |
| c.strip("-•. \t") | |
| for c in content.split("\n") | |
| if c.strip() and not c.lower().startswith("description")] | |
| # Remove duplicates and empty lines, preserve order | |
| seen = set() | |
| final = [] | |
| for c in concepts: | |
| if c.lower() not in seen and c: | |
| seen.add(c.lower()) | |
| final.append(c) | |
| if len(final) >= top_k: | |
| break | |
| # Render the output with stock links for each concept | |
| if not final: | |
| return "No conceptual keywords generated." | |
| out_md = "" | |
| for c in final: | |
| getty = get_stock_url("getty", c) | |
| adobe = get_stock_url("adobe", c) | |
| out_md += f"**{c}** \n[Getty Images]({getty}) | [Adobe Stock]({adobe})\n\n" | |
| return out_md | |
| with gr.Blocks(title="LLM-Powered Conceptual Keyword Tool (Groq API)") as demo: | |
| gr.Markdown(""" | |
| Guten Luck | |
| """) | |
| api_box = gr.Textbox(label="Groq API Key (required)", type="password") | |
| inp = gr.Textbox(lines=4, label="Describe your scene, event, or concept…") | |
| submit = gr.Button("🔍 Generate Conceptual Keywords") | |
| out = gr.Markdown("Suggestions will appear here.") | |
| submit.click( | |
| groq_conceptual_keywords, | |
| inputs=[api_box, inp], | |
| outputs=out | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |