Spaces:
Sleeping
Sleeping
File size: 5,559 Bytes
8524303 8e6393b 2519b2c 8524303 8e6393b 78eab1a 8e6393b 44617e0 2519b2c 78eab1a 2519b2c 8e6393b 44617e0 2519b2c 44617e0 2519b2c 44617e0 8e6393b 44617e0 2519b2c 8e6393b 44617e0 8e6393b 2519b2c 78eab1a 8e6393b 2519b2c 6388e9c 8e6393b 6388e9c 2519b2c 78eab1a 6388e9c 8e6393b 6388e9c 8e6393b 6388e9c 44617e0 2519b2c 6388e9c 44617e0 2519b2c 44617e0 2519b2c 8e6393b 44617e0 78eab1a 44617e0 2519b2c 44617e0 2519b2c 44617e0 2519b2c 44617e0 2519b2c 44617e0 78eab1a 2519b2c 44617e0 2519b2c 44617e0 2519b2c 78eab1a 2519b2c 6388e9c 2519b2c 8e6393b 2519b2c 44617e0 2519b2c 8524303 44617e0 78eab1a 44617e0 | 1 2 3 4 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
import re
# Load the multilingual LLM (FLAN-T5 base) for conversational tasks
model_name = "google/flan-t5-base"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer, max_length=128)
def generate_llm_response(message):
"""Generate response using FLAN-T5 with multilingual prompting"""
if not message.strip():
return "Please say something!"
# Detect if the input is in Nepali
is_nepali = bool(re.search(r'[\u0900-\u097F]', message))
# Craft a prompt based on language detection
if is_nepali:
prompt = f"तपाईं एक नेपाली च्याटबोट हुनुहुन्छ। प्रयोगकर्ताले भनेको कुराको जवाफ नेपालीमा दिनुहोस्: {message}"
else:
prompt = f"You are a friendly chatbot that can respond in English or Nepali. Respond to the user's message: {message}"
# Generate response
response = generator(prompt, max_length=128, num_return_sequences=1, temperature=0.7)[0]['generated_text']
# Post-process to ensure a complete sentence
response = response.strip()
if not response.endswith(('.', '!', '?')):
response += "।" if is_nepali else "."
return response
def chat_function(message, history):
"""Main chat interface function"""
if not message.strip():
return history, ""
# Generate response
bot_response = generate_llm_response(message)
# Add to history
history.append([message, bot_response])
return history, ""
# Custom CSS
css = """
.gradio-container {
max-width: 800px !important;
margin: auto !important;
background-color: #1a1a2e !important;
}
.message.user {
background-color: #e3f2fd !important;
border-radius: 15px !important;
padding: 10px !important;
color: #1e1e1e !important;
}
.message.bot {
background-color: #d1d1d1 !important;
border-radius: 15px !important;
padding: 10px !important;
color: #1e1e1e !important;
}
.chatbot .message {
color: #1e1e1e !important;
}
.input-container {
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%) !important;
border-radius: 25px !important;
}
.input-container input {
color: #ffffff !important;
background: transparent !important;
}
.gradio-chatbot {
background-color: #16213e !important;
}
"""
# Create the Gradio interface
with gr.Blocks(css=css, title="Simple Nepali Chatbot", theme=gr.themes.Default()) as demo:
gr.HTML("""
<div style="text-align: center; padding: 20px;">
<h1>🇳🇵 नेपाली च्याटबोट</h1>
<h2>Simple Nepali Chatbot</h2>
<p style="font-size: 18px;">
<strong>नेपालीमा वा अंग्रेजीमा कुराकानी गर्नुहोस्!</strong><br>
<em>Chat in Nepali or English!</em>
</p>
</div>
""")
chatbot_ui = gr.Chatbot(
value=None,
height=400,
show_label=False,
container=True,
bubble_full_width=False,
show_copy_button=True
)
with gr.Row():
msg_input = gr.Textbox(
placeholder="यहाँ लेख्नुहोस् / Type here...",
show_label=False,
scale=4,
lines=1,
container=False
)
send_btn = gr.Button("📤 Send", scale=1, variant="primary")
clear_btn = gr.Button("🗑️ Clear", scale=1, variant="secondary")
# Example conversations
with gr.Row():
gr.Examples(
examples=[
["नमस्ते!"],
["Hello!"],
["तपाईंको नाम के हो?"],
["How are you?"],
["What is your name?"],
["कस्तो छ?"],
["Thank you!"],
["धन्यवाद!"]
],
inputs=msg_input,
label="🔄 Try these examples / यी उदाहरणहरू प्रयास गर्नुहोस्"
)
# Event handlers
msg_input.submit(
chat_function,
inputs=[msg_input, chatbot_ui],
outputs=[chatbot_ui, msg_input]
)
send_btn.click(
chat_function,
inputs=[msg_input, chatbot_ui],
outputs=[chatbot_ui, msg_input]
)
clear_btn.click(
lambda: ([], ""),
outputs=[chatbot_ui, msg_input]
)
gr.HTML("""
<div style="text-align: center; margin-top: 20px; padding: 20px; background: #16213e; border-radius: 10px; color: #ffffff;">
<h3>📝 About this Chatbot</h3>
<p>This is a simple LLM-based chatbot that responds in both Nepali and English.</p>
<p><strong>यो एक सरल LLM-आधारित च्याटबोट हो जसले नेपाली र अंग्रेजी दुवैमा जवाफ दिन्छ।</strong></p>
<p><em>Powered by a lightweight model - works on Hugging Face Spaces! ⚡</em></p>
</div>
""")
# Launch the app
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
) |