Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ from openai import OpenAI
|
|
| 3 |
from ddgs import DDGS
|
| 4 |
import os
|
| 5 |
import base64
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# --- 1. CONFIGURATION ---
|
| 8 |
|
|
@@ -19,10 +21,15 @@ nebius_client = OpenAI(
|
|
| 19 |
base_url=NEBIUS_BASE_URL,
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# --- 2. DEFINE LOCAL TOOLS ---
|
| 23 |
|
| 24 |
def tool_generate_text(prompt: str, system_message: str = "You are a helpful assistant.") -> str:
|
| 25 |
-
"""
|
| 26 |
print(f"📝 Text Tool calling API: {prompt[:40]}...")
|
| 27 |
try:
|
| 28 |
response = nebius_client.chat.completions.create(
|
|
@@ -41,7 +48,7 @@ def tool_generate_text(prompt: str, system_message: str = "You are a helpful ass
|
|
| 41 |
|
| 42 |
|
| 43 |
def tool_generate_image(prompt: str):
|
| 44 |
-
"""
|
| 45 |
print(f"🎨 Image Tool calling API: {prompt[:40]}...")
|
| 46 |
try:
|
| 47 |
response = nebius_client.images.generate(
|
|
@@ -49,12 +56,14 @@ def tool_generate_image(prompt: str):
|
|
| 49 |
prompt=prompt,
|
| 50 |
n=1,
|
| 51 |
size="1024x1024",
|
| 52 |
-
response_format="b64_json",
|
| 53 |
)
|
| 54 |
|
| 55 |
b64_data = response.data[0].b64_json
|
| 56 |
image_bytes = base64.b64decode(b64_data)
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
print(f"❌ Image Tool Error: {e}")
|
|
@@ -62,7 +71,7 @@ def tool_generate_image(prompt: str):
|
|
| 62 |
|
| 63 |
|
| 64 |
def tool_web_search(query: str) -> str:
|
| 65 |
-
"""
|
| 66 |
print(f"🔍 Search Tool searching for: {query}...")
|
| 67 |
try:
|
| 68 |
results = DDGS().text(query, max_results=4)
|
|
@@ -86,14 +95,14 @@ def tool_web_search(query: str) -> str:
|
|
| 86 |
# --- 3. AGENT LOGIC ---
|
| 87 |
|
| 88 |
def run_autonomous_agent(company_name, business_type, style, progress=gr.Progress()):
|
| 89 |
-
"""
|
| 90 |
|
| 91 |
# PHASE 1: PLANNING
|
| 92 |
progress(0.1, desc="Step 1: Planning & Color Palette...")
|
| 93 |
|
| 94 |
color_prompt = (
|
| 95 |
f"Create a distinct, 5-color palette for a {business_type} named '{company_name}' "
|
| 96 |
-
f"with a {style} style. Return ONLY a comma-separated list of
|
| 97 |
)
|
| 98 |
|
| 99 |
colors_text = tool_generate_text(
|
|
@@ -109,24 +118,24 @@ def run_autonomous_agent(company_name, business_type, style, progress=gr.Progres
|
|
| 109 |
|
| 110 |
logo_prompt = (
|
| 111 |
f"A professional, minimalist logo for a {business_type} named '{company_name}'. "
|
| 112 |
-
f"
|
| 113 |
)
|
| 114 |
|
| 115 |
logo_img = tool_generate_image(logo_prompt)
|
| 116 |
log += "🖼️ Logo Generated.\n"
|
| 117 |
|
| 118 |
-
# PHASE 3:
|
| 119 |
-
progress(0.5, desc="Step 3: Generating
|
| 120 |
|
| 121 |
asset_prompt = (
|
| 122 |
-
f"A professional
|
| 123 |
-
f"
|
| 124 |
)
|
| 125 |
|
| 126 |
asset_img = tool_generate_image(asset_prompt)
|
| 127 |
log += "🖼️ Brand Asset Generated.\n"
|
| 128 |
|
| 129 |
-
# PHASE 4: SEARCH
|
| 130 |
progress(0.7, desc="Step 4: Researching Market Trends...")
|
| 131 |
|
| 132 |
search_query = f"marketing trends for {business_type} 2025"
|
|
@@ -140,8 +149,7 @@ def run_autonomous_agent(company_name, business_type, style, progress=gr.Progres
|
|
| 140 |
|
| 141 |
copy_prompt = f"""
|
| 142 |
Write 3 engaging Twitter/X posts for a new {business_type} called '{company_name}'.
|
| 143 |
-
Tone: {style}
|
| 144 |
-
|
| 145 |
Use these market insights if helpful:
|
| 146 |
{search_data}
|
| 147 |
"""
|
|
@@ -171,12 +179,14 @@ def chat_response(message, history):
|
|
| 171 |
model=TEXT_MODEL,
|
| 172 |
max_tokens=300,
|
| 173 |
)
|
|
|
|
| 174 |
bot_reply = response.choices[0].message.content
|
| 175 |
|
| 176 |
history.append({"role": "user", "content": message})
|
| 177 |
history.append({"role": "assistant", "content": bot_reply})
|
| 178 |
|
| 179 |
return history, ""
|
|
|
|
| 180 |
except Exception as e:
|
| 181 |
history.append({"role": "assistant", "content": f"Error: {e}"})
|
| 182 |
return history, ""
|
|
|
|
| 3 |
from ddgs import DDGS
|
| 4 |
import os
|
| 5 |
import base64
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
|
| 9 |
# --- 1. CONFIGURATION ---
|
| 10 |
|
|
|
|
| 21 |
base_url=NEBIUS_BASE_URL,
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Convert image bytes → PIL Image
|
| 25 |
+
def bytes_to_pil(image_bytes: bytes):
|
| 26 |
+
return Image.open(io.BytesIO(image_bytes))
|
| 27 |
+
|
| 28 |
+
|
| 29 |
# --- 2. DEFINE LOCAL TOOLS ---
|
| 30 |
|
| 31 |
def tool_generate_text(prompt: str, system_message: str = "You are a helpful assistant.") -> str:
|
| 32 |
+
"""Text generation tool."""
|
| 33 |
print(f"📝 Text Tool calling API: {prompt[:40]}...")
|
| 34 |
try:
|
| 35 |
response = nebius_client.chat.completions.create(
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def tool_generate_image(prompt: str):
|
| 51 |
+
"""Image tool that returns PIL Image (not bytes)."""
|
| 52 |
print(f"🎨 Image Tool calling API: {prompt[:40]}...")
|
| 53 |
try:
|
| 54 |
response = nebius_client.images.generate(
|
|
|
|
| 56 |
prompt=prompt,
|
| 57 |
n=1,
|
| 58 |
size="1024x1024",
|
| 59 |
+
response_format="b64_json",
|
| 60 |
)
|
| 61 |
|
| 62 |
b64_data = response.data[0].b64_json
|
| 63 |
image_bytes = base64.b64decode(b64_data)
|
| 64 |
+
|
| 65 |
+
# FIX: return PIL Image so Gradio can render it
|
| 66 |
+
return bytes_to_pil(image_bytes)
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
print(f"❌ Image Tool Error: {e}")
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
def tool_web_search(query: str) -> str:
|
| 74 |
+
"""Web search using DuckDuckGo."""
|
| 75 |
print(f"🔍 Search Tool searching for: {query}...")
|
| 76 |
try:
|
| 77 |
results = DDGS().text(query, max_results=4)
|
|
|
|
| 95 |
# --- 3. AGENT LOGIC ---
|
| 96 |
|
| 97 |
def run_autonomous_agent(company_name, business_type, style, progress=gr.Progress()):
|
| 98 |
+
"""Autonomous branding agent."""
|
| 99 |
|
| 100 |
# PHASE 1: PLANNING
|
| 101 |
progress(0.1, desc="Step 1: Planning & Color Palette...")
|
| 102 |
|
| 103 |
color_prompt = (
|
| 104 |
f"Create a distinct, 5-color palette for a {business_type} named '{company_name}' "
|
| 105 |
+
f"with a {style} style. Return ONLY a comma-separated list of hex codes."
|
| 106 |
)
|
| 107 |
|
| 108 |
colors_text = tool_generate_text(
|
|
|
|
| 118 |
|
| 119 |
logo_prompt = (
|
| 120 |
f"A professional, minimalist logo for a {business_type} named '{company_name}'. "
|
| 121 |
+
f"Use ONLY these colors: {colors_text}. Vector flat style, centered, no text."
|
| 122 |
)
|
| 123 |
|
| 124 |
logo_img = tool_generate_image(logo_prompt)
|
| 125 |
log += "🖼️ Logo Generated.\n"
|
| 126 |
|
| 127 |
+
# PHASE 3: COLOR PALETTE ASSET
|
| 128 |
+
progress(0.5, desc="Step 3: Generating Color Palette Guide...")
|
| 129 |
|
| 130 |
asset_prompt = (
|
| 131 |
+
f"A professional color palette guide with 5 circular swatches, arranged in a row. "
|
| 132 |
+
f"Use these colors: {colors_text}. Minimalist white background."
|
| 133 |
)
|
| 134 |
|
| 135 |
asset_img = tool_generate_image(asset_prompt)
|
| 136 |
log += "🖼️ Brand Asset Generated.\n"
|
| 137 |
|
| 138 |
+
# PHASE 4: WEB SEARCH
|
| 139 |
progress(0.7, desc="Step 4: Researching Market Trends...")
|
| 140 |
|
| 141 |
search_query = f"marketing trends for {business_type} 2025"
|
|
|
|
| 149 |
|
| 150 |
copy_prompt = f"""
|
| 151 |
Write 3 engaging Twitter/X posts for a new {business_type} called '{company_name}'.
|
| 152 |
+
Tone: {style}.
|
|
|
|
| 153 |
Use these market insights if helpful:
|
| 154 |
{search_data}
|
| 155 |
"""
|
|
|
|
| 179 |
model=TEXT_MODEL,
|
| 180 |
max_tokens=300,
|
| 181 |
)
|
| 182 |
+
|
| 183 |
bot_reply = response.choices[0].message.content
|
| 184 |
|
| 185 |
history.append({"role": "user", "content": message})
|
| 186 |
history.append({"role": "assistant", "content": bot_reply})
|
| 187 |
|
| 188 |
return history, ""
|
| 189 |
+
|
| 190 |
except Exception as e:
|
| 191 |
history.append({"role": "assistant", "content": f"Error: {e}"})
|
| 192 |
return history, ""
|