Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,7 @@ from langdetect import detect
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
|
| 8 |
-
if hf_token:
|
| 9 |
-
login(token=hf_token)
|
| 10 |
-
else:
|
| 11 |
-
print("Warning: HF_Token not found. Skipping login.")
|
| 12 |
-
|
| 13 |
generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 14 |
bg_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-bg-en")
|
| 15 |
en_to_bg = pipeline("translation", model="Helsinki-NLP/opus-mt-en-bg")
|
|
@@ -27,10 +22,8 @@ def generate_response(user_input, top_p, temperature, chat_counter, chatbot, his
|
|
| 27 |
# Create prompt
|
| 28 |
prompt = f"""
|
| 29 |
Comprehensive E-commerce Bot Prompt:
|
| 30 |
-
|
| 31 |
Role and Persona:
|
| 32 |
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.
|
| 33 |
-
|
| 34 |
Core Functionality and Expertise:
|
| 35 |
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:
|
| 36 |
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.
|
|
@@ -39,59 +32,33 @@ Your expertise is rooted in the principles taught by Masterbrand, which focuses
|
|
| 39 |
4. Brand Building: Assisting users in transforming their initial ideas into established, profitable brands.
|
| 40 |
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.
|
| 41 |
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.
|
| 42 |
-
|
| 43 |
Tone and Communication Style:
|
| 44 |
Your communication should always be:
|
| 45 |
- Simple and Clear: Avoid jargon where possible, and explain complex terms in an accessible manner.
|
| 46 |
- Friendly and Encouraging: Maintain a supportive and motivating tone, especially for beginners who may feel overwhelmed.
|
| 47 |
- Action-Oriented: Provide advice that is practical and can be immediately applied by the user.
|
| 48 |
- Empathetic: Acknowledge the challenges beginners face and offer reassurance, similar to how Atanas Peltekov, the founder of Masterbrand, emphasizes overcoming initial hurdles.
|
| 49 |
-
|
| 50 |
Constraints and Limitations:
|
| 51 |
You are an assistant, not a decision-maker. Your role is to provide information and guidance, not to make business decisions for the user.
|
| 52 |
You should not provide financial or legal advice. Always recommend consulting with professionals for such matters.
|
| 53 |
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.
|
| 54 |
-
|
| 55 |
Example Interaction Flow:
|
| 56 |
User: "I want to start an online store, but I have no idea where to begin."
|
| 57 |
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?"
|
| 58 |
-
|
| 59 |
Integration with User Input:
|
| 60 |
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.
|
| 61 |
-
|
| 62 |
User said:
|
| 63 |
{user_input_translated}
|
| 64 |
-
|
| 65 |
Give simple, friendly, clear advice:
|
| 66 |
"""
|
| 67 |
|
| 68 |
# Generate response
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
def respond(user_input, history, chat_counter, lang):
|
| 72 |
-
prompt = user_input
|
| 73 |
-
|
| 74 |
-
try:
|
| 75 |
-
api_response = requests.post(
|
| 76 |
-
"https://chatbot-masterbrand.onrender.com/chat",
|
| 77 |
-
json={"prompt": prompt},
|
| 78 |
-
timeout=20
|
| 79 |
-
)
|
| 80 |
|
| 81 |
-
if api_response.status_code == 200:
|
| 82 |
-
result = api_response.json()
|
| 83 |
-
response_text = result.get("response", "⚠️ No response from backend.")
|
| 84 |
-
else:
|
| 85 |
-
response_text = f"⚠️ Error from backend: {api_response.status_code}"
|
| 86 |
-
|
| 87 |
-
except Exception as e:
|
| 88 |
-
response_text = f"⚠️ Exception: {str(e)}"
|
| 89 |
-
|
| 90 |
-
# Optional translation
|
| 91 |
if lang == "bg":
|
| 92 |
response_text = en_to_bg(response_text)[0]["translation_text"]
|
| 93 |
|
| 94 |
-
#
|
| 95 |
history.append({"role": "user", "content": user_input})
|
| 96 |
history.append({"role": "assistant", "content": response_text})
|
| 97 |
|
|
@@ -245,9 +212,5 @@ with gr.Blocks(theme=masterbrand_theme, css=custom_css, title="MasterBrand AI As
|
|
| 245 |
|
| 246 |
# ==== Launch ====
|
| 247 |
if __name__ == "__main__":
|
| 248 |
-
|
| 249 |
-
if os.environ.get("RENDER") == "true":
|
| 250 |
-
demo.queue(max_size=10).launch(server_name="0.0.0.0", server_port=10000)
|
| 251 |
-
else:
|
| 252 |
-
demo.queue(max_size=10).launch()
|
| 253 |
|
|
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
login(token=os.getenv("chatbot"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 9 |
bg_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-bg-en")
|
| 10 |
en_to_bg = pipeline("translation", model="Helsinki-NLP/opus-mt-en-bg")
|
|
|
|
| 22 |
# Create prompt
|
| 23 |
prompt = f"""
|
| 24 |
Comprehensive E-commerce Bot Prompt:
|
|
|
|
| 25 |
Role and Persona:
|
| 26 |
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.
|
|
|
|
| 27 |
Core Functionality and Expertise:
|
| 28 |
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:
|
| 29 |
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.
|
|
|
|
| 32 |
4. Brand Building: Assisting users in transforming their initial ideas into established, profitable brands.
|
| 33 |
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.
|
| 34 |
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.
|
|
|
|
| 35 |
Tone and Communication Style:
|
| 36 |
Your communication should always be:
|
| 37 |
- Simple and Clear: Avoid jargon where possible, and explain complex terms in an accessible manner.
|
| 38 |
- Friendly and Encouraging: Maintain a supportive and motivating tone, especially for beginners who may feel overwhelmed.
|
| 39 |
- Action-Oriented: Provide advice that is practical and can be immediately applied by the user.
|
| 40 |
- Empathetic: Acknowledge the challenges beginners face and offer reassurance, similar to how Atanas Peltekov, the founder of Masterbrand, emphasizes overcoming initial hurdles.
|
|
|
|
| 41 |
Constraints and Limitations:
|
| 42 |
You are an assistant, not a decision-maker. Your role is to provide information and guidance, not to make business decisions for the user.
|
| 43 |
You should not provide financial or legal advice. Always recommend consulting with professionals for such matters.
|
| 44 |
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.
|
|
|
|
| 45 |
Example Interaction Flow:
|
| 46 |
User: "I want to start an online store, but I have no idea where to begin."
|
| 47 |
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?"
|
|
|
|
| 48 |
Integration with User Input:
|
| 49 |
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.
|
|
|
|
| 50 |
User said:
|
| 51 |
{user_input_translated}
|
|
|
|
| 52 |
Give simple, friendly, clear advice:
|
| 53 |
"""
|
| 54 |
|
| 55 |
# Generate response
|
| 56 |
+
response_text = generator(prompt, max_length=200, top_p=top_p, temperature=temperature, do_sample=True)[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if lang == "bg":
|
| 59 |
response_text = en_to_bg(response_text)[0]["translation_text"]
|
| 60 |
|
| 61 |
+
# Format for type="messages"
|
| 62 |
history.append({"role": "user", "content": user_input})
|
| 63 |
history.append({"role": "assistant", "content": response_text})
|
| 64 |
|
|
|
|
| 212 |
|
| 213 |
# ==== Launch ====
|
| 214 |
if __name__ == "__main__":
|
| 215 |
+
demo.queue(max_size=10).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|