Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,93 @@
|
|
| 1 |
-
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
outputs = summarization_model.generate(**inputs)
|
| 34 |
-
summarized_text = summarization_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
-
return summarized_text
|
| 36 |
-
|
| 37 |
-
# Function to perform paraphrasing
|
| 38 |
-
def paraphrase_text(input_text):
|
| 39 |
-
inputs = paraphrase_tokenizer(input_text, return_tensors="pt")
|
| 40 |
-
outputs = paraphrase_model.generate(**inputs)
|
| 41 |
-
paraphrased_text = paraphrase_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 42 |
-
return paraphrased_text
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# Define the Gradio interface
|
| 55 |
iface = gr.Interface(
|
| 56 |
-
fn=
|
| 57 |
-
inputs=[
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
live=False,
|
| 60 |
-
title="
|
|
|
|
| 61 |
theme='Ajaxon6255/Emerald_Isle',
|
| 62 |
examples=[
|
| 63 |
-
["
|
| 64 |
-
["
|
| 65 |
-
["
|
| 66 |
-
["তিনজনের কেউই বাবার পথ ধরে প্রযুক্তি দুনিয়ায় হাঁটেননি।"],
|
| 67 |
-
["বিশ্বের আরও একটি সেরা ক্লাব। স্প্যানিশ ফুটবলে বার্সেলোনারই প্রবল প্রতিদ্বন্দ্বী রিয়াল মাদ্রিদ। লিওনেল মেসির ক্লাব ইতিহাসে উয়েফা চ্যাম্পিয়নস লিগ জিতেছে মোট পাঁচবার। ইউরোপীয় কাপ উইনার্স কাপে তাদের সাফল্য আছে চারবার। পাশাপাশি তিনবার ক্লাব বিশ্বকাপ জেতা বার্সা লা লিগায় সাফল্য পেয়েছেন মোট ২৪ বার। ডিয়েগো ম্যারাডোনা, ইয়োহান ক্রুইফ, রিভালদোদের মতো খেলোয়াড়েরা এই ক্লাবের হয়ে অংশ হয়ে আছেন ইতিহাসের। "]
|
| 68 |
-
|
| 69 |
]
|
| 70 |
)
|
| 71 |
|
| 72 |
# Launch the Gradio app
|
| 73 |
-
iface.launch(inline=False)
|
|
|
|
| 1 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# Load a conversational model (replace with your Bangla LLM)
|
| 5 |
+
chatbot_model_name = "csebuetnlp/banglat5_base" # Replace with your fine-tuned model
|
| 6 |
+
chatbot_tokenizer = AutoTokenizer.from_pretrained(chatbot_model_name)
|
| 7 |
+
chatbot_model = AutoModelForSeq2SeqLM.from_pretrained(chatbot_model_name)
|
| 8 |
|
| 9 |
+
# Function to generate empathetic responses
|
| 10 |
+
def generate_empathetic_response(user_input):
|
| 11 |
+
try:
|
| 12 |
+
# Prepare the input for the chatbot
|
| 13 |
+
input_text = f"User: {user_input}\nChatbot:"
|
| 14 |
+
inputs = chatbot_tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 15 |
+
|
| 16 |
+
# Generate response
|
| 17 |
+
outputs = chatbot_model.generate(
|
| 18 |
+
**inputs,
|
| 19 |
+
max_length=512,
|
| 20 |
+
num_beams=5,
|
| 21 |
+
early_stopping=True,
|
| 22 |
+
no_repeat_ngram_size=2
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Decode and clean the response
|
| 26 |
+
response = chatbot_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 27 |
+
|
| 28 |
+
# Ensure the response is culturally appropriate and empathetic
|
| 29 |
+
response = add_empathy_and_cultural_tone(response)
|
| 30 |
+
return response
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error generating response: {str(e)}"
|
| 33 |
|
| 34 |
+
# Function to add empathy and cultural tone to the response
|
| 35 |
+
def add_empathy_and_cultural_tone(response):
|
| 36 |
+
# Add empathetic phrases
|
| 37 |
+
empathetic_phrases = [
|
| 38 |
+
"বুঝতে পারছি", "কষ্ট পেয়েছো নিশ্চয়", "আমি তোমার পাশে আছি",
|
| 39 |
+
"এটা খুব কঠিন সময় তোমার জন্য", "তোমার অনুভূতি আমি বুঝতে পারছি"
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
# Add regional dialects and informal pauses
|
| 43 |
+
regional_phrases = [
|
| 44 |
+
"ভাইয়া", "হুজুর", "থাইকা", "পাল্লায় পড়া", "টাকা ফুরাইয়া গেছে",
|
| 45 |
+
"পকেট শূন্য", "মনটা ভাঙা কাঁচের মতো"
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
# Randomly add empathetic and regional phrases to the response
|
| 49 |
+
import random
|
| 50 |
+
if random.random() > 0.5:
|
| 51 |
+
response = random.choice(empathetic_phrases) + "। " + response
|
| 52 |
+
if random.random() > 0.5:
|
| 53 |
+
response = response + " " + random.choice(regional_phrases)
|
| 54 |
+
|
| 55 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# Function to handle the chatbot conversation
|
| 58 |
+
def chat_with_bot(user_input, history=[]):
|
| 59 |
+
# Generate a response
|
| 60 |
+
bot_response = generate_empathetic_response(user_input)
|
| 61 |
+
|
| 62 |
+
# Update conversation history
|
| 63 |
+
history.append((user_input, bot_response))
|
| 64 |
+
|
| 65 |
+
# Format the conversation history for display
|
| 66 |
+
formatted_history = "\n".join([f"User: {user}\nChatbot: {bot}" for user, bot in history])
|
| 67 |
+
|
| 68 |
+
return formatted_history, history
|
| 69 |
|
| 70 |
# Define the Gradio interface
|
| 71 |
iface = gr.Interface(
|
| 72 |
+
fn=chat_with_bot,
|
| 73 |
+
inputs=[
|
| 74 |
+
gr.Textbox(label="Your Message", placeholder="আপনার সমস্যা বা অনুভূতি শেয়ার করুন..."),
|
| 75 |
+
gr.State() # To store conversation history
|
| 76 |
+
],
|
| 77 |
+
outputs=[
|
| 78 |
+
gr.Textbox(label="Conversation History", lines=10),
|
| 79 |
+
gr.State() # To pass updated history back
|
| 80 |
+
],
|
| 81 |
live=False,
|
| 82 |
+
title="Bangla Emotional Support Chatbot",
|
| 83 |
+
description="আপনার সমস্যা বা অনুভূতি শেয়ার করুন। আমি আপনার পাশে আছি।",
|
| 84 |
theme='Ajaxon6255/Emerald_Isle',
|
| 85 |
examples=[
|
| 86 |
+
["আমার টাকা ফুরিয়ে গেছে এবং আমি ঋণে ডুবে আছি।"],
|
| 87 |
+
["আমার ভাইয়ের সাথে আমার সম্পর্ক খারাপ হয়ে গেছে।"],
|
| 88 |
+
["আমি খুব একা বোধ করছি এবং কাউকে পাশে পাচ্ছি না।"]
|
|
|
|
|
|
|
|
|
|
| 89 |
]
|
| 90 |
)
|
| 91 |
|
| 92 |
# Launch the Gradio app
|
| 93 |
+
iface.launch(inline=False)
|