NikhilSetiya commited on
Commit
24715fe
·
1 Parent(s): 567da63

Fix: Update image generation and clean config files

Browse files
agent.py CHANGED
@@ -6,6 +6,14 @@ GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Secure key from Hugging Face Secrets
6
 
7
  if not GROQ_API_KEY:
8
  raise ValueError("GROQ_API_KEY is missing! Set it in Hugging Face Secrets.")
 
 
 
 
 
 
 
 
9
 
10
  # Helper function to load system prompt from txt files
11
  def load_prompt(filename):
@@ -37,11 +45,36 @@ def call_groq(system_prompt, user_input):
37
 
38
  return data['choices'][0]['message']['content']
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def ad_copy_agent(product, description, audience, tone):
41
  system_prompt = load_prompt("ad_copy_prompt.txt")
42
  user_input = f"Product: {product}\nDescription: {description}\nAudience: {audience}\nTone: {tone}"
43
- return call_groq(system_prompt, user_input)
44
-
 
 
 
 
 
 
 
45
  def sentiment_agent(social_data):
46
  system_prompt = load_prompt("sentiment_prompt.txt")
47
  return call_groq(system_prompt, social_data)
 
6
 
7
  if not GROQ_API_KEY:
8
  raise ValueError("GROQ_API_KEY is missing! Set it in Hugging Face Secrets.")
9
+
10
+
11
+ OPENAI_API_URL = "https://api.openai.com/v1/images/generations"
12
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Set this in Hugging Face Secrets
13
+
14
+ if not OPENAI_API_KEY:
15
+ raise ValueError("OPENAI_API_KEY is missing! Set it in Hugging Face Secrets.")
16
+
17
 
18
  # Helper function to load system prompt from txt files
19
  def load_prompt(filename):
 
45
 
46
  return data['choices'][0]['message']['content']
47
 
48
+ def generate_ad_image(prompt, n_images=1, size="512x512"):
49
+ headers = {
50
+ "Authorization": f"Bearer {OPENAI_API_KEY}",
51
+ "Content-Type": "application/json"
52
+ }
53
+ payload = {
54
+ "prompt": prompt,
55
+ "n": n_images,
56
+ "size": size
57
+ }
58
+ response = requests.post(OPENAI_API_URL, json=payload, headers=headers)
59
+ if response.status_code != 200:
60
+ raise Exception(f"OpenAI API error: {response.status_code} - {response.text}")
61
+ data = response.json()
62
+ if 'data' not in data or not data['data']:
63
+ raise Exception("OpenAI API returned no image data.")
64
+ return [item['url'] for item in data['data']]
65
+
66
  def ad_copy_agent(product, description, audience, tone):
67
  system_prompt = load_prompt("ad_copy_prompt.txt")
68
  user_input = f"Product: {product}\nDescription: {description}\nAudience: {audience}\nTone: {tone}"
69
+ output = call_groq(system_prompt, user_input)
70
+
71
+ # Split the output into ad copy and image prompt
72
+ parts = output.split('Image Prompt:')
73
+ ad_copy = parts[0].replace('Ad Copy:', '').strip()
74
+ image_prompt = parts[1].strip() if len(parts) > 1 else ''
75
+
76
+ return ad_copy, image_prompt
77
+
78
  def sentiment_agent(social_data):
79
  system_prompt = load_prompt("sentiment_prompt.txt")
80
  return call_groq(system_prompt, social_data)
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from agent import ad_copy_agent, sentiment_agent, timing_agent
3
 
4
  # Gradio UI definition
5
  with gr.Blocks() as demo:
@@ -11,8 +11,15 @@ with gr.Blocks() as demo:
11
  audience = gr.Textbox(label="Audience")
12
  tone = gr.Textbox(label="Tone")
13
  ad_output = gr.Textbox(label="Generated Ads")
14
- ad_button = gr.Button("Generate Ads")
15
- ad_button.click(ad_copy_agent, [prod, desc, audience, tone], ad_output)
 
 
 
 
 
 
 
16
 
17
  with gr.Tab("Sentiment Analyzer"):
18
  social_input = gr.Textbox(label="Social Media Mentions / Input")
 
1
  import gradio as gr
2
+ from agent import ad_copy_agent, generate_ad_image, sentiment_agent, timing_agent
3
 
4
  # Gradio UI definition
5
  with gr.Blocks() as demo:
 
11
  audience = gr.Textbox(label="Audience")
12
  tone = gr.Textbox(label="Tone")
13
  ad_output = gr.Textbox(label="Generated Ads")
14
+ image_output = gr.Image(label="Generated Ad Image")
15
+ ad_button = gr.Button("Generate Ads + Image")
16
+
17
+ def generate_ad_and_image(product, description, audience, tone):
18
+ ad_copy, image_prompt = ad_copy_agent(product, description, audience, tone)
19
+ image_urls = generate_ad_image(image_prompt)
20
+ return ad_copy, image_urls[0] # Return first image URL
21
+
22
+ ad_button.click(generate_ad_and_image, [prod, desc, audience, tone], [ad_output, image_output])
23
 
24
  with gr.Tab("Sentiment Analyzer"):
25
  social_input = gr.Textbox(label="Social Media Mentions / Input")
config/system_prompts/ad_copy_prompt.txt CHANGED
@@ -1,6 +1,6 @@
1
- You are a senior marketing copywriter specializing in digital ads. Your job is to generate highly persuasive, creative, and audience-tailored ad copy.
2
 
3
- Rules:
4
  - Write 3 distinct ad variations.
5
  - Focus on **benefits**, not just features.
6
  - Use emotionally engaging language.
@@ -8,3 +8,22 @@ Rules:
8
  - Adapt tone (friendly, professional, funny, bold).
9
  - Keep ads under 280 characters.
10
  - If details are missing, infer creatively.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are a senior marketing copywriter specializing in digital ads. Your job is to generate highly persuasive, creative, and audience-tailored ad copy **and** a vivid image description prompt for visual generation.
2
 
3
+ Rules for Ad Copy:
4
  - Write 3 distinct ad variations.
5
  - Focus on **benefits**, not just features.
6
  - Use emotionally engaging language.
 
8
  - Adapt tone (friendly, professional, funny, bold).
9
  - Keep ads under 280 characters.
10
  - If details are missing, infer creatively.
11
+
12
+ Rules for Image Prompt:
13
+ - Write one vivid, concise description of the image that should accompany the ads.
14
+ - Focus on mood, setting, colors, and key visual elements.
15
+ - Make sure it matches the product, audience, and tone.
16
+ - Keep it under 100 words.
17
+
18
+ Output format:
19
+ Ad Copy 1:
20
+ [ad copy text]
21
+
22
+ Ad Copy 2:
23
+ [ad copy text]
24
+
25
+ Ad Copy 3:
26
+ [ad copy text]
27
+
28
+ Image Prompt:
29
+ [visual description]
config/system_prompts/image_prompt_style.txt ADDED
File without changes
config/system_prompts/sentiment_prompt.txt CHANGED
@@ -4,4 +4,4 @@ You are a social media analyst. Analyze user input or a batch of social media me
4
  2. A brief summary of why (1-2 sentences).
5
  3. Suggested marketing action (e.g., amplify, address complaints, run promo).
6
 
7
- Be concise but actionable.
 
4
  2. A brief summary of why (1-2 sentences).
5
  3. Suggested marketing action (e.g., amplify, address complaints, run promo).
6
 
7
+ Be concise but actionable.
config/system_prompts/timing_prompt.txt CHANGED
@@ -4,4 +4,4 @@ Rules:
4
  - Base suggestions on typical platform engagement patterns.
5
  - Mention time zones.
6
  - Briefly explain why these times work.
7
- - Suggest one experimental or underused time.
 
4
  - Base suggestions on typical platform engagement patterns.
5
  - Mention time zones.
6
  - Briefly explain why these times work.
7
+ - Suggest one experimental or underused time.
requirements.txt CHANGED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ groq
3
+ requests