omarash2016 commited on
Commit
99b5081
·
verified ·
1 Parent(s): ec9aeda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -8
app.py CHANGED
@@ -5,6 +5,7 @@ import os
5
  import base64
6
  import io
7
  from PIL import Image
 
8
 
9
  # --- 1. CONFIGURATION ---
10
  NEBIUS_API_KEY = os.environ.get("NEBIUS_API_KEY")
@@ -40,7 +41,7 @@ def tool_generate_image(prompt: str):
40
  n=1,
41
  size="1024x1024",
42
  response_format="b64_json",
43
- style="flat",
44
  )
45
  b64_data = response.data[0].b64_json
46
  image_bytes = base64.b64decode(b64_data)
@@ -119,11 +120,17 @@ def run_autonomous_agent(company_name, business_type, style, progress=gr.Progres
119
 
120
  # --- 4. BRAND CONSULTANT CHATBOT ---
121
  def chat_response(message, history):
 
122
  system_message = "You are a creative brand consultant."
123
  messages = [{"role": "system", "content": system_message}]
 
 
124
  for msg in history:
125
- messages.append({"role": msg["role"], "content": msg["content"]})
 
 
126
  messages.append({"role": "user", "content": message})
 
127
  try:
128
  response = nebius_client.chat.completions.create(
129
  messages=messages,
@@ -131,6 +138,10 @@ def chat_response(message, history):
131
  max_tokens=300,
132
  )
133
  bot_reply = response.choices[0].message.content
 
 
 
 
134
  history.append({"role": "user", "content": message})
135
  history.append({"role": "assistant", "content": bot_reply})
136
  return history, ""
@@ -138,8 +149,10 @@ def chat_response(message, history):
138
  history.append({"role": "assistant", "content": f"Error: {e}"})
139
  return history, ""
140
 
141
- # --- 5. GRADIO 6 UI ---
142
- with gr.Blocks() as demo:
 
 
143
  # --- Custom CSS for colors & style ---
144
  gr.HTML("""
145
  <style>
@@ -152,7 +165,7 @@ with gr.Blocks() as demo:
152
  # HEADER
153
  with gr.Column(elem_id="main-header"):
154
  gr.Markdown("# 🖼️ AutoBrand Studio")
155
- gr.Markdown("Autonomous Agent powered by AI Models, MCP Tools & Web Search. Connect Claude Desktop to use these tools!")
156
 
157
  with gr.Row():
158
  # LEFT PANEL
@@ -161,7 +174,9 @@ with gr.Blocks() as demo:
161
  company_input = gr.Textbox(label="Company Name", value="Lumina")
162
  type_input = gr.Textbox(label="Business Type", value="Organic Candle Shop")
163
  style_input = gr.Textbox(label="Style / Vibe", value="Minimalist, calming")
 
164
  generate_btn = gr.Button("✨ Generate Brand Kit", variant="primary")
 
165
  gr.Markdown("### ⚙️ System Logs")
166
  out_log = gr.TextArea(label="Agent Log", lines=15)
167
 
@@ -175,7 +190,8 @@ with gr.Blocks() as demo:
175
  out_copy = gr.TextArea(label="Generated Social Content", lines=15, interactive=False)
176
 
177
  with gr.TabItem("💬 Brand Consultant"):
178
- chatbot = gr.Chatbot(label="AI Consultant", height=650)
 
179
  chat_input = gr.Textbox(show_label=False, placeholder="Ask something...")
180
  chat_btn = gr.Button("Send")
181
 
@@ -196,7 +212,7 @@ with gr.Blocks() as demo:
196
  chat_btn.click(chat_response, [chat_input, chatbot], [chatbot, chat_input])
197
  chat_input.submit(chat_response, [chat_input, chatbot], [chatbot, chat_input])
198
 
199
- # MCP TOOLS (Hidden)
200
  with gr.Row(visible=False):
201
  search_in = gr.Textbox()
202
  search_out = gr.Textbox()
@@ -214,4 +230,6 @@ with gr.Blocks() as demo:
214
  btn_summarize.click(tool_summarize_market_insights, inputs=summarize_in, outputs=summarize_out, api_name="summarize_market_insights")
215
 
216
  if __name__ == "__main__":
217
- demo.launch(title="AutoBrand MCP Studio", ssr_mode=False, mcp_server=True)
 
 
 
5
  import base64
6
  import io
7
  from PIL import Image
8
+ from typing import List, Dict
9
 
10
  # --- 1. CONFIGURATION ---
11
  NEBIUS_API_KEY = os.environ.get("NEBIUS_API_KEY")
 
41
  n=1,
42
  size="1024x1024",
43
  response_format="b64_json",
44
+ style="flat", # Note: Check if your API supports this specific param, standard OpenAI doesn't always
45
  )
46
  b64_data = response.data[0].b64_json
47
  image_bytes = base64.b64decode(b64_data)
 
120
 
121
  # --- 4. BRAND CONSULTANT CHATBOT ---
122
  def chat_response(message, history):
123
+ # History in Gradio 'type="messages"' is a list of dicts: [{'role':'user', 'content':'...'}, ...]
124
  system_message = "You are a creative brand consultant."
125
  messages = [{"role": "system", "content": system_message}]
126
+
127
+ # Append existing history
128
  for msg in history:
129
+ messages.append(msg)
130
+
131
+ # Append current message
132
  messages.append({"role": "user", "content": message})
133
+
134
  try:
135
  response = nebius_client.chat.completions.create(
136
  messages=messages,
 
138
  max_tokens=300,
139
  )
140
  bot_reply = response.choices[0].message.content
141
+
142
+ # In Gradio type="messages", we return the text for the specific turn if we are using an iterator,
143
+ # OR we return the updated history list.
144
+ # Here we manually update history and return it + empty string for input clearing.
145
  history.append({"role": "user", "content": message})
146
  history.append({"role": "assistant", "content": bot_reply})
147
  return history, ""
 
149
  history.append({"role": "assistant", "content": f"Error: {e}"})
150
  return history, ""
151
 
152
+ # --- 5. GRADIO UI ---
153
+ # Moved 'title' here inside Blocks()
154
+ with gr.Blocks(title="AutoBrand MCP Studio", theme=gr.themes.Soft()) as demo:
155
+
156
  # --- Custom CSS for colors & style ---
157
  gr.HTML("""
158
  <style>
 
165
  # HEADER
166
  with gr.Column(elem_id="main-header"):
167
  gr.Markdown("# 🖼️ AutoBrand Studio")
168
+ gr.Markdown("Autonomous Agent powered by AI Models, MCP Tools & Web Search.")
169
 
170
  with gr.Row():
171
  # LEFT PANEL
 
174
  company_input = gr.Textbox(label="Company Name", value="Lumina")
175
  type_input = gr.Textbox(label="Business Type", value="Organic Candle Shop")
176
  style_input = gr.Textbox(label="Style / Vibe", value="Minimalist, calming")
177
+
178
  generate_btn = gr.Button("✨ Generate Brand Kit", variant="primary")
179
+
180
  gr.Markdown("### ⚙️ System Logs")
181
  out_log = gr.TextArea(label="Agent Log", lines=15)
182
 
 
190
  out_copy = gr.TextArea(label="Generated Social Content", lines=15, interactive=False)
191
 
192
  with gr.TabItem("💬 Brand Consultant"):
193
+ # Updated to type="messages" to support List[Dict] history
194
+ chatbot = gr.Chatbot(label="AI Consultant", height=600, type="messages")
195
  chat_input = gr.Textbox(show_label=False, placeholder="Ask something...")
196
  chat_btn = gr.Button("Send")
197
 
 
212
  chat_btn.click(chat_response, [chat_input, chatbot], [chatbot, chat_input])
213
  chat_input.submit(chat_response, [chat_input, chatbot], [chatbot, chat_input])
214
 
215
+ # MCP TOOLS (Hidden elements to expose API endpoints)
216
  with gr.Row(visible=False):
217
  search_in = gr.Textbox()
218
  search_out = gr.Textbox()
 
230
  btn_summarize.click(tool_summarize_market_insights, inputs=summarize_in, outputs=summarize_out, api_name="summarize_market_insights")
231
 
232
  if __name__ == "__main__":
233
+ # Removed 'title' from launch()
234
+ # Removed 'mcp_server' (unless you are using a specific custom fork, standard Gradio launch doesn't take this kwarg)
235
+ demo.launch(ssr_mode=False)