Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,22 +4,83 @@ from langdetect import detect
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import os
|
| 6 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
login(token=os.getenv("chatbot"))
|
| 9 |
generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 10 |
bg_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-bg-en")
|
| 11 |
en_to_bg = pipeline("translation", model="Helsinki-NLP/opus-mt-en-bg")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def generate_response(user_input, top_p, temperature, chat_counter, chatbot, history, request: gr.Request):
|
| 14 |
lang = detect(user_input)
|
| 15 |
print(f"Detected language: {lang}")
|
| 16 |
|
| 17 |
# Translate if needed
|
| 18 |
if lang == "bg":
|
| 19 |
user_input_translated = bg_to_en(user_input)[0]["translation_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
else:
|
| 21 |
user_input_translated = user_input
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Create prompt
|
| 24 |
prompt = f"""
|
| 25 |
Comprehensive E-commerce Bot Prompt:
|
|
@@ -301,6 +362,9 @@ with gr.Blocks(theme=masterbrand_theme, css=custom_css, title="MasterBrand AI As
|
|
| 301 |
|
| 302 |
inputs = gr.Textbox(
|
| 303 |
placeholder="Ask me anything about your e-commerce business... 💡",
|
|
|
|
|
|
|
|
|
|
| 304 |
label="Your Question",
|
| 305 |
lines=3,
|
| 306 |
elem_classes=["input-container"],
|
|
@@ -359,9 +423,19 @@ with gr.Blocks(theme=masterbrand_theme, css=custom_css, title="MasterBrand AI As
|
|
| 359 |
|
| 360 |
# Event Handlers
|
| 361 |
inputs.submit(reset_textbox, [], [inputs, submit_btn], queue=False)
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
|
| 366 |
# ==== Launch ====
|
| 367 |
if __name__ == "__main__":
|
|
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import os
|
| 6 |
import base64
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 9 |
+
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}")
|
| 26 |
|
| 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:
|
| 36 |
+
img_desc = describe_image(image)
|
| 37 |
+
prompt += f"[Image Description]: {img_desc}\n"
|
| 38 |
+
except Exception as e:
|
| 39 |
+
prompt += f"[Image Error]: {str(e)}\n"
|
| 40 |
+
|
| 41 |
+
if video is not None:
|
| 42 |
+
try:
|
| 43 |
+
vid_desc = describe_video(video)
|
| 44 |
+
prompt += f"[Video Description]: {vid_desc}\n"
|
| 45 |
+
except Exception as e:
|
| 46 |
+
prompt += f"[Video Error]: {str(e)}\n"
|
| 47 |
+
|
| 48 |
+
if url:
|
| 49 |
+
url_content = extract_text_from_url(url)
|
| 50 |
+
prompt += f"[URL Content]: {url_content}\n"
|
| 51 |
+
|
| 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:
|
|
|
|
| 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"],
|
|
|
|
| 423 |
|
| 424 |
# Event Handlers
|
| 425 |
inputs.submit(reset_textbox, [], [inputs, submit_btn], queue=False)
|
| 426 |
+
inputs.submit(
|
| 427 |
+
generate_response,
|
| 428 |
+
[inputs, top_p, temperature, chat_counter, chatbot, state, image_input, video_input, url_input],
|
| 429 |
+
[chatbot, state, chat_counter, status_box, inputs, submit_btn]
|
| 430 |
+
)
|
| 431 |
+
|
| 432 |
+
submit_btn.click(reset_textbox, [], [inputs, submit_btn], queue=False)
|
| 433 |
+
submit_btn.click(
|
| 434 |
+
generate_response,
|
| 435 |
+
[inputs, top_p, temperature, chat_counter, chatbot, state, image_input, video_input, url_input],
|
| 436 |
+
[chatbot, state, chat_counter, status_box, inputs, submit_btn]
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
|
| 440 |
# ==== Launch ====
|
| 441 |
if __name__ == "__main__":
|