Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -156,6 +156,20 @@ def romanized_to_bengali(text: str) -> str:
|
|
| 156 |
|
| 157 |
return text_lower
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
def create_chat_interface():
|
| 160 |
# Custom CSS for better styling
|
| 161 |
custom_css = """
|
|
@@ -206,7 +220,8 @@ def create_chat_interface():
|
|
| 206 |
height=500,
|
| 207 |
show_label=False,
|
| 208 |
container=True,
|
| 209 |
-
elem_classes=["chat-window"]
|
|
|
|
| 210 |
)
|
| 211 |
|
| 212 |
# Input area with buttons
|
|
@@ -219,11 +234,6 @@ def create_chat_interface():
|
|
| 219 |
send_btn = gr.Button("Send", variant="primary")
|
| 220 |
clear_btn = gr.Button("Clear")
|
| 221 |
|
| 222 |
-
# Additional features bar
|
| 223 |
-
with gr.Row():
|
| 224 |
-
audio_input = gr.Audio(source="microphone", type="filepath", label="Voice Input")
|
| 225 |
-
image_output = gr.Image(label="Generated Image", visible=False)
|
| 226 |
-
|
| 227 |
# Settings panel (collapsible)
|
| 228 |
with gr.Accordion("Advanced Settings", open=False):
|
| 229 |
with gr.Row():
|
|
@@ -259,20 +269,20 @@ def create_chat_interface():
|
|
| 259 |
# Function to handle sending messages
|
| 260 |
def user_message(message, history):
|
| 261 |
if message:
|
| 262 |
-
return "", history + [
|
| 263 |
return "", history
|
| 264 |
|
| 265 |
def bot_response(history, system_msg, max_tokens, temperature, top_p):
|
| 266 |
-
if
|
| 267 |
return history
|
| 268 |
|
| 269 |
# Get the last user message
|
| 270 |
-
message = history[-1][
|
| 271 |
|
| 272 |
# Check for custom responses first
|
| 273 |
custom_response = check_custom_responses(message)
|
| 274 |
if custom_response:
|
| 275 |
-
history
|
| 276 |
return history
|
| 277 |
|
| 278 |
# Check for image generation request
|
|
@@ -280,35 +290,34 @@ def create_chat_interface():
|
|
| 280 |
try:
|
| 281 |
image = generate_image(message)
|
| 282 |
if image:
|
| 283 |
-
history
|
| 284 |
-
# Handle image display logic
|
| 285 |
return history
|
| 286 |
except Exception as e:
|
| 287 |
-
history
|
| 288 |
return history
|
| 289 |
|
| 290 |
# Handle regular text responses
|
| 291 |
try:
|
| 292 |
translated_msg, original_lang, was_transliterated = translate_text(message)
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
system_msg,
|
| 297 |
-
max_tokens,
|
| 298 |
-
temperature,
|
| 299 |
-
top_p
|
| 300 |
-
)
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
yield history
|
| 308 |
-
time.sleep(0.02)
|
| 309 |
|
| 310 |
except Exception as e:
|
| 311 |
-
history
|
| 312 |
yield history
|
| 313 |
|
| 314 |
# Event handlers
|
|
@@ -336,17 +345,6 @@ def create_chat_interface():
|
|
| 336 |
|
| 337 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 338 |
|
| 339 |
-
# Handle voice input
|
| 340 |
-
def process_audio(audio_file):
|
| 341 |
-
# Add your audio transcription logic here
|
| 342 |
-
return "Audio input received! (Add your transcription logic)"
|
| 343 |
-
|
| 344 |
-
audio_input.change(
|
| 345 |
-
process_audio,
|
| 346 |
-
inputs=[audio_input],
|
| 347 |
-
outputs=[txt]
|
| 348 |
-
)
|
| 349 |
-
|
| 350 |
return demo
|
| 351 |
|
| 352 |
# Create and launch the interface
|
|
|
|
| 156 |
|
| 157 |
return text_lower
|
| 158 |
|
| 159 |
+
import gradio as gr
|
| 160 |
+
import time
|
| 161 |
+
from huggingface_hub import InferenceClient
|
| 162 |
+
from deep_translator import GoogleTranslator
|
| 163 |
+
from indic_transliteration import sanscript
|
| 164 |
+
from indic_transliteration.detect import detect as detect_script
|
| 165 |
+
from indic_transliteration.sanscript import transliterate
|
| 166 |
+
import langdetect
|
| 167 |
+
import re
|
| 168 |
+
|
| 169 |
+
# Initialize clients
|
| 170 |
+
text_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 171 |
+
image_client = InferenceClient("SG161222/RealVisXL_V3.0")
|
| 172 |
+
|
| 173 |
def create_chat_interface():
|
| 174 |
# Custom CSS for better styling
|
| 175 |
custom_css = """
|
|
|
|
| 220 |
height=500,
|
| 221 |
show_label=False,
|
| 222 |
container=True,
|
| 223 |
+
elem_classes=["chat-window"],
|
| 224 |
+
type='messages' # Fixed: Added type parameter
|
| 225 |
)
|
| 226 |
|
| 227 |
# Input area with buttons
|
|
|
|
| 234 |
send_btn = gr.Button("Send", variant="primary")
|
| 235 |
clear_btn = gr.Button("Clear")
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
# Settings panel (collapsible)
|
| 238 |
with gr.Accordion("Advanced Settings", open=False):
|
| 239 |
with gr.Row():
|
|
|
|
| 269 |
# Function to handle sending messages
|
| 270 |
def user_message(message, history):
|
| 271 |
if message:
|
| 272 |
+
return "", history + [{"role": "user", "content": message}]
|
| 273 |
return "", history
|
| 274 |
|
| 275 |
def bot_response(history, system_msg, max_tokens, temperature, top_p):
|
| 276 |
+
if not history:
|
| 277 |
return history
|
| 278 |
|
| 279 |
# Get the last user message
|
| 280 |
+
message = history[-1]["content"]
|
| 281 |
|
| 282 |
# Check for custom responses first
|
| 283 |
custom_response = check_custom_responses(message)
|
| 284 |
if custom_response:
|
| 285 |
+
history.append({"role": "assistant", "content": custom_response})
|
| 286 |
return history
|
| 287 |
|
| 288 |
# Check for image generation request
|
|
|
|
| 290 |
try:
|
| 291 |
image = generate_image(message)
|
| 292 |
if image:
|
| 293 |
+
history.append({"role": "assistant", "content": "Here's your generated image!"})
|
|
|
|
| 294 |
return history
|
| 295 |
except Exception as e:
|
| 296 |
+
history.append({"role": "assistant", "content": f"Sorry, I couldn't generate the image: {str(e)}"})
|
| 297 |
return history
|
| 298 |
|
| 299 |
# Handle regular text responses
|
| 300 |
try:
|
| 301 |
translated_msg, original_lang, was_transliterated = translate_text(message)
|
| 302 |
+
messages = [{"role": "system", "content": system_msg}]
|
| 303 |
+
messages.extend(history[:-1])
|
| 304 |
+
messages.append({"role": "user", "content": translated_msg})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
+
response = ""
|
| 307 |
+
for token in text_client.chat_completion(
|
| 308 |
+
messages,
|
| 309 |
+
max_tokens=max_tokens,
|
| 310 |
+
temperature=temperature,
|
| 311 |
+
top_p=top_p,
|
| 312 |
+
stream=True,
|
| 313 |
+
):
|
| 314 |
+
response += token.choices[0].delta.content or ""
|
| 315 |
+
history.append({"role": "assistant", "content": response})
|
| 316 |
yield history
|
| 317 |
+
time.sleep(0.02)
|
| 318 |
|
| 319 |
except Exception as e:
|
| 320 |
+
history.append({"role": "assistant", "content": f"An error occurred: {str(e)}"})
|
| 321 |
yield history
|
| 322 |
|
| 323 |
# Event handlers
|
|
|
|
| 345 |
|
| 346 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 347 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
return demo
|
| 349 |
|
| 350 |
# Create and launch the interface
|