Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,16 +10,44 @@ import requests
|
|
| 10 |
from bs4 import BeautifulSoup
|
| 11 |
import cv2
|
| 12 |
from io import BytesIO
|
|
|
|
| 13 |
|
| 14 |
login(token=os.getenv("chatbot"))
|
| 15 |
generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 16 |
bg_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-bg-en")
|
| 17 |
en_to_bg = pipeline("translation", model="Helsinki-NLP/opus-mt-en-bg")
|
|
|
|
| 18 |
# Load BLIP for image captioning
|
| 19 |
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 20 |
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 21 |
blip_model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def generate_response(user_input, top_p, temperature, chat_counter, chatbot, history, image=None, video=None, url=None, request: gr.Request = None):
|
| 24 |
lang = detect(user_input)
|
| 25 |
print(f"Detected language: {lang}")
|
|
@@ -27,9 +55,11 @@ def generate_response(user_input, top_p, temperature, chat_counter, chatbot, his
|
|
| 27 |
# Translate if needed
|
| 28 |
if lang == "bg":
|
| 29 |
user_input_translated = bg_to_en(user_input)[0]["translation_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
prompt = ""
|
| 32 |
-
|
| 33 |
# Multimodal additions
|
| 34 |
if image is not None:
|
| 35 |
try:
|
|
@@ -52,68 +82,6 @@ def generate_response(user_input, top_p, temperature, chat_counter, chatbot, his
|
|
| 52 |
# Append user message to prompt
|
| 53 |
prompt += f"User said:\n{user_input_translated}\n\nGive simple, friendly, clear advice:\n"
|
| 54 |
|
| 55 |
-
else:
|
| 56 |
-
user_input_translated = user_input
|
| 57 |
-
|
| 58 |
-
def describe_image(image):
|
| 59 |
-
inputs = blip_processor(images=image, return_tensors="pt").to(blip_model.device)
|
| 60 |
-
out = blip_model.generate(**inputs)
|
| 61 |
-
caption = blip_processor.decode(out[0], skip_special_tokens=True)
|
| 62 |
-
return caption
|
| 63 |
-
|
| 64 |
-
def describe_video(video_path):
|
| 65 |
-
cap = cv2.VideoCapture(video_path)
|
| 66 |
-
success, frame = cap.read()
|
| 67 |
-
cap.release()
|
| 68 |
-
if success:
|
| 69 |
-
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 70 |
-
return describe_image(img)
|
| 71 |
-
else:
|
| 72 |
-
return "Could not read video frame."
|
| 73 |
-
|
| 74 |
-
def extract_text_from_url(url):
|
| 75 |
-
try:
|
| 76 |
-
response = requests.get(url, timeout=5)
|
| 77 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 78 |
-
paragraphs = soup.find_all('p')
|
| 79 |
-
text = ' '.join([p.text for p in paragraphs])
|
| 80 |
-
return text[:2000] # keep it short
|
| 81 |
-
except Exception as e:
|
| 82 |
-
return f"Failed to fetch URL: {str(e)}"
|
| 83 |
-
|
| 84 |
-
# Create prompt
|
| 85 |
-
prompt = f"""
|
| 86 |
-
Comprehensive E-commerce Bot Prompt:
|
| 87 |
-
Role and Persona:
|
| 88 |
-
You are an exceptionally helpful, friendly, and clear e-commerce business assistant, specifically designed for beginners and aspiring entrepreneurs. Your primary goal is to guide users through the process of establishing, developing, and scaling their online businesses, drawing upon the proven strategies and real-world experience embodied by the Masterbrand e-commerce training program. You should embody the spirit of a seasoned mentor who simplifies complex e-commerce concepts into actionable, easy-to-understand advice.
|
| 89 |
-
Core Functionality and Expertise:
|
| 90 |
-
Your expertise is rooted in the principles taught by Masterbrand, which focuses on building successful e-commerce brands from the ground up. You are proficient in:
|
| 91 |
-
1. Foundational E-commerce Setup: Guiding users on how to start an e-commerce brand from scratch, including initial ideation, niche selection, and basic platform setup.
|
| 92 |
-
2. Business Development and Growth: Providing insights into developing online businesses, implementing effective growth strategies, and structuring a business for long-term success.
|
| 93 |
-
3. Strategic Implementation: Offering practical advice on applying working strategies and business structures that lead to real sales and tangible results.
|
| 94 |
-
4. Brand Building: Assisting users in transforming their initial ideas into established, profitable brands.
|
| 95 |
-
5. Market Context (Bulgaria): While your advice is universally applicable, you understand the nuances of the e-commerce landscape, particularly in regions like Bulgaria, given Masterbrand\'s origin as the #1 ECOM training in Bulgaria.
|
| 96 |
-
6. Learning Resource Guidance: You can refer to the types of resources found in Masterbrand (e.g., video lessons, modules, practical resources) to explain concepts or suggest learning paths.
|
| 97 |
-
Tone and Communication Style:
|
| 98 |
-
Your communication should always be:
|
| 99 |
-
- Simple and Clear: Avoid jargon where possible, and explain complex terms in an accessible manner.
|
| 100 |
-
- Friendly and Encouraging: Maintain a supportive and motivating tone, especially for beginners who may feel overwhelmed.
|
| 101 |
-
- Action-Oriented: Provide advice that is practical and can be immediately applied by the user.
|
| 102 |
-
- Empathetic: Acknowledge the challenges beginners face and offer reassurance, similar to how Atanas Peltekov, the founder of Masterbrand, emphasizes overcoming initial hurdles.
|
| 103 |
-
Constraints and Limitations:
|
| 104 |
-
You are an assistant, not a decision-maker. Your role is to provide information and guidance, not to make business decisions for the user.
|
| 105 |
-
You should not provide financial or legal advice. Always recommend consulting with professionals for such matters.
|
| 106 |
-
Your knowledge is based on the principles of successful e-commerce as observed and taught by Masterbrand. While comprehensive, it is not exhaustive of all possible e-commerce strategies.
|
| 107 |
-
Example Interaction Flow:
|
| 108 |
-
User: "I want to start an online store, but I have no idea where to begin."
|
| 109 |
-
Bot: "That\'s a fantastic goal! Many successful e-commerce brands started exactly where you are now. To begin, let\'s think about what you\'re passionate about or what problem you want to solve for customers. This will help us choose a niche. Once we have a niche, we can explore simple ways to set up your first online presence. What kind of products or services are you considering?"
|
| 110 |
-
Integration with User Input:
|
| 111 |
-
Always consider the user\'s specific input and tailor your advice accordingly. If the user provides a specific question or scenario, address it directly while integrating the core principles of e-commerce success.
|
| 112 |
-
User said:
|
| 113 |
-
{user_input_translated}
|
| 114 |
-
Give simple, friendly, clear advice:
|
| 115 |
-
"""
|
| 116 |
-
|
| 117 |
# Generate response
|
| 118 |
response_text = generator(prompt, max_length=200, top_p=top_p, temperature=temperature, do_sample=True)[0]["generated_text"]
|
| 119 |
|
|
@@ -125,10 +93,12 @@ Give simple, friendly, clear advice:
|
|
| 125 |
history.append({"role": "assistant", "content": response_text})
|
| 126 |
|
| 127 |
chat_counter += 1
|
| 128 |
-
return history, history, chat_counter, "✅ Success", gr.update(value
|
|
|
|
| 129 |
|
| 130 |
def reset_textbox():
|
| 131 |
-
return gr.update(value
|
|
|
|
| 132 |
|
| 133 |
# ==== Enhanced Custom CSS with Masterbrand Styling ====
|
| 134 |
custom_css = f"""
|
|
@@ -191,7 +161,7 @@ custom_css = f"""
|
|
| 191 |
background: rgba(26, 26, 26, 0.95) !important;
|
| 192 |
border: 2px solid #ff0000 !important; /* Changed to red */
|
| 193 |
border-radius: 25px !important;
|
| 194 |
-
box-shadow: 0 15px 40px rgba(255, 0, 0, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.1) !important;
|
| 195 |
margin-bottom: 2rem !important;
|
| 196 |
backdrop-filter: blur(10px) !important;
|
| 197 |
}}
|
|
@@ -209,7 +179,7 @@ custom_css = f"""
|
|
| 209 |
}}
|
| 210 |
|
| 211 |
.input-container textarea {{
|
| 212 |
-
background: linear-gradient(135deg, #1a1a1a 0%, #
|
| 213 |
border: 2px solid #ff0000 !important; /* Changed to red */
|
| 214 |
border-radius: 15px !important;
|
| 215 |
color: #ffffff !important;
|
|
@@ -287,7 +257,7 @@ custom_css = f"""
|
|
| 287 |
|
| 288 |
/* Enhanced chat styling */
|
| 289 |
.message.user {{
|
| 290 |
-
background: linear-gradient(135deg, #2a2a2a 0%, #
|
| 291 |
border-left: 4px solid #ff0000 !important; /* Changed to red */
|
| 292 |
margin-left: 2rem !important;
|
| 293 |
}}
|
|
@@ -362,15 +332,17 @@ with gr.Blocks(theme=masterbrand_theme, css=custom_css, title="MasterBrand AI As
|
|
| 362 |
|
| 363 |
inputs = gr.Textbox(
|
| 364 |
placeholder="Ask me anything about your e-commerce business... 💡",
|
| 365 |
-
image_input = gr.Image(label="Upload an Image (optional)", type="pil", optional=True)
|
| 366 |
-
video_input = gr.Video(label="Upload a Video (optional)", optional=True)
|
| 367 |
-
url_input = gr.Textbox(label="Paste a URL (optional)", placeholder="https://example.com", optional=True)
|
| 368 |
label="Your Question",
|
| 369 |
lines=3,
|
| 370 |
elem_classes=["input-container"],
|
| 371 |
show_label=True
|
| 372 |
)
|
| 373 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
with gr.Row():
|
| 375 |
submit_btn = gr.Button(
|
| 376 |
"🚀 Get Expert Advice",
|
|
@@ -423,18 +395,18 @@ with gr.Blocks(theme=masterbrand_theme, css=custom_css, title="MasterBrand AI As
|
|
| 423 |
|
| 424 |
# Event Handlers
|
| 425 |
inputs.submit(reset_textbox, [], [inputs, submit_btn], queue=False)
|
| 426 |
-
inputs.submit(
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
)
|
| 431 |
|
| 432 |
-
submit_btn.click(reset_textbox, [], [inputs, submit_btn], queue=False)
|
| 433 |
-
submit_btn.click(
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
)
|
| 438 |
|
| 439 |
|
| 440 |
# ==== Launch ====
|
|
@@ -442,3 +414,4 @@ if __name__ == "__main__":
|
|
| 442 |
demo.queue(max_size=10).launch(server_name="0.0.0.0", server_port=7860)
|
| 443 |
|
| 444 |
|
|
|
|
|
|
| 10 |
from bs4 import BeautifulSoup
|
| 11 |
import cv2
|
| 12 |
from io import BytesIO
|
| 13 |
+
import torch
|
| 14 |
|
| 15 |
login(token=os.getenv("chatbot"))
|
| 16 |
generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 17 |
bg_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-bg-en")
|
| 18 |
en_to_bg = pipeline("translation", model="Helsinki-NLP/opus-mt-en-bg")
|
| 19 |
+
|
| 20 |
# Load BLIP for image captioning
|
| 21 |
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 22 |
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 23 |
blip_model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
|
| 25 |
+
def describe_image(image):
|
| 26 |
+
inputs = blip_processor(images=image, return_tensors="pt").to(blip_model.device)
|
| 27 |
+
out = blip_model.generate(**inputs)
|
| 28 |
+
caption = blip_processor.decode(out[0], skip_special_tokens=True)
|
| 29 |
+
return caption
|
| 30 |
+
|
| 31 |
+
def describe_video(video_path):
|
| 32 |
+
cap = cv2.VideoCapture(video_path)
|
| 33 |
+
success, frame = cap.read()
|
| 34 |
+
cap.release()
|
| 35 |
+
if success:
|
| 36 |
+
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 37 |
+
return describe_image(img)
|
| 38 |
+
else:
|
| 39 |
+
return "Could not read video frame."
|
| 40 |
+
|
| 41 |
+
def extract_text_from_url(url):
|
| 42 |
+
try:
|
| 43 |
+
response = requests.get(url, timeout=5)
|
| 44 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 45 |
+
paragraphs = soup.find_all('p')
|
| 46 |
+
text = ' '.join([p.text for p in paragraphs])
|
| 47 |
+
return text[:2000] # keep it short
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Failed to fetch URL: {str(e)}"
|
| 50 |
+
|
| 51 |
def generate_response(user_input, top_p, temperature, chat_counter, chatbot, history, image=None, video=None, url=None, request: gr.Request = None):
|
| 52 |
lang = detect(user_input)
|
| 53 |
print(f"Detected language: {lang}")
|
|
|
|
| 55 |
# Translate if needed
|
| 56 |
if lang == "bg":
|
| 57 |
user_input_translated = bg_to_en(user_input)[0]["translation_text"]
|
| 58 |
+
else:
|
| 59 |
+
user_input_translated = user_input
|
| 60 |
+
|
| 61 |
+
prompt = ""
|
| 62 |
|
|
|
|
|
|
|
| 63 |
# Multimodal additions
|
| 64 |
if image is not None:
|
| 65 |
try:
|
|
|
|
| 82 |
# Append user message to prompt
|
| 83 |
prompt += f"User said:\n{user_input_translated}\n\nGive simple, friendly, clear advice:\n"
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
# Generate response
|
| 86 |
response_text = generator(prompt, max_length=200, top_p=top_p, temperature=temperature, do_sample=True)[0]["generated_text"]
|
| 87 |
|
|
|
|
| 93 |
history.append({"role": "assistant", "content": response_text})
|
| 94 |
|
| 95 |
chat_counter += 1
|
| 96 |
+
return history, history, chat_counter, "✅ Success", gr.update(value=\'\
|
| 97 |
+
'), gr.update(interactive=True)
|
| 98 |
|
| 99 |
def reset_textbox():
|
| 100 |
+
return gr.update(value=\'\
|
| 101 |
+
'), gr.update(interactive=False)
|
| 102 |
|
| 103 |
# ==== Enhanced Custom CSS with Masterbrand Styling ====
|
| 104 |
custom_css = f"""
|
|
|
|
| 161 |
background: rgba(26, 26, 26, 0.95) !important;
|
| 162 |
border: 2px solid #ff0000 !important; /* Changed to red */
|
| 163 |
border-radius: 25px !important;
|
| 164 |
+
box-shadow: 0 15px 40px rgba(255, 0, 0, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.1) !important; /* Changed to red */
|
| 165 |
margin-bottom: 2rem !important;
|
| 166 |
backdrop-filter: blur(10px) !important;
|
| 167 |
}}
|
|
|
|
| 179 |
}}
|
| 180 |
|
| 181 |
.input-container textarea {{
|
| 182 |
+
background: linear-gradient(135deg, #1a1a1a 0%, #2a2a1a 100%) !important;
|
| 183 |
border: 2px solid #ff0000 !important; /* Changed to red */
|
| 184 |
border-radius: 15px !important;
|
| 185 |
color: #ffffff !important;
|
|
|
|
| 257 |
|
| 258 |
/* Enhanced chat styling */
|
| 259 |
.message.user {{
|
| 260 |
+
background: linear-gradient(135deg, #2a2a2a 0%, #3a3a2a 100%) !important;
|
| 261 |
border-left: 4px solid #ff0000 !important; /* Changed to red */
|
| 262 |
margin-left: 2rem !important;
|
| 263 |
}}
|
|
|
|
| 332 |
|
| 333 |
inputs = gr.Textbox(
|
| 334 |
placeholder="Ask me anything about your e-commerce business... 💡",
|
|
|
|
|
|
|
|
|
|
| 335 |
label="Your Question",
|
| 336 |
lines=3,
|
| 337 |
elem_classes=["input-container"],
|
| 338 |
show_label=True
|
| 339 |
)
|
| 340 |
|
| 341 |
+
image_input = gr.Image(label="Upload an Image (optional)", type="pil", optional=True)
|
| 342 |
+
video_input = gr.Video(label="Upload a Video (optional)", optional=True)
|
| 343 |
+
url_input = gr.Textbox(label="Paste a URL (optional)", placeholder="https://example.com", optional=True)
|
| 344 |
+
|
| 345 |
+
|
| 346 |
with gr.Row():
|
| 347 |
submit_btn = gr.Button(
|
| 348 |
"🚀 Get Expert Advice",
|
|
|
|
| 395 |
|
| 396 |
# Event Handlers
|
| 397 |
inputs.submit(reset_textbox, [], [inputs, submit_btn], queue=False)
|
| 398 |
+
inputs.submit(
|
| 399 |
+
generate_response,
|
| 400 |
+
[inputs, top_p, temperature, chat_counter, chatbot, state, image_input, video_input, url_input],
|
| 401 |
+
[chatbot, state, chat_counter, status_box, inputs, submit_btn]
|
| 402 |
+
)
|
| 403 |
|
| 404 |
+
submit_btn.click(reset_textbox, [], [inputs, submit_btn], queue=False)
|
| 405 |
+
submit_btn.click(
|
| 406 |
+
generate_response,
|
| 407 |
+
[inputs, top_p, temperature, chat_counter, chatbot, state, image_input, video_input, url_input],
|
| 408 |
+
[chatbot, state, chat_counter, status_box, inputs, submit_btn]
|
| 409 |
+
)
|
| 410 |
|
| 411 |
|
| 412 |
# ==== Launch ====
|
|
|
|
| 414 |
demo.queue(max_size=10).launch(server_name="0.0.0.0", server_port=7860)
|
| 415 |
|
| 416 |
|
| 417 |
+
|