Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,145 +27,113 @@ text_client = InferenceClient(
|
|
| 27 |
API_URL = "https://api-inference.huggingface.co/models/SG161222/RealVisXL_V4.0"
|
| 28 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
"""Detect language
|
| 32 |
-
Returns (language_code, script_type)"""
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
script
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
except:
|
| 46 |
-
pass
|
| 47 |
-
return lang, script
|
| 48 |
except:
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
"""
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
]
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
return
|
| 66 |
|
| 67 |
-
def
|
| 68 |
-
"""
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
return text, 'en', False
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
return translated, original_lang, is_transliterated
|
| 82 |
-
except Exception as e:
|
| 83 |
-
print(f"Translation error: {e}")
|
| 84 |
-
return text, 'en', False
|
| 85 |
|
| 86 |
-
|
| 87 |
-
if original_lang == 'en' and len(text.split()) > 2 and is_romanized_indic(text):
|
| 88 |
-
text = romanized_to_bengali(text)
|
| 89 |
-
return translate_text(text, target_lang) # Recursive call with Bengali script
|
| 90 |
|
| 91 |
-
return text, 'en', False
|
| 92 |
-
|
| 93 |
-
def check_custom_responses(message: str) -> str:
|
| 94 |
-
"""Check for specific patterns and return custom responses."""
|
| 95 |
-
message_lower = message.lower()
|
| 96 |
-
custom_responses = {
|
| 97 |
-
"what is ur name?": "xylaria",
|
| 98 |
-
"what is your name?": "xylaria",
|
| 99 |
-
"what's your name?": "xylaria",
|
| 100 |
-
"whats your name": "xylaria",
|
| 101 |
-
"how many 'r' is in strawberry?": "3",
|
| 102 |
-
"who is your developer?": "sk md saad amin",
|
| 103 |
-
"how many r is in strawberry": "3",
|
| 104 |
-
"who is ur dev": "sk md saad amin",
|
| 105 |
-
"who is ur developer": "sk md saad amin",
|
| 106 |
-
}
|
| 107 |
-
for pattern, response in custom_responses.items():
|
| 108 |
-
if pattern in message_lower:
|
| 109 |
-
return response
|
| 110 |
-
return None
|
| 111 |
-
|
| 112 |
-
def is_image_request(message: str) -> bool:
|
| 113 |
-
"""Detect if the message is requesting image generation."""
|
| 114 |
-
image_triggers = [
|
| 115 |
-
"generate an image",
|
| 116 |
-
"create an image",
|
| 117 |
-
"draw",
|
| 118 |
-
"make a picture",
|
| 119 |
-
"generate a picture",
|
| 120 |
-
"create a picture",
|
| 121 |
-
"generate art",
|
| 122 |
-
"create art",
|
| 123 |
-
"make art",
|
| 124 |
-
"visualize",
|
| 125 |
-
"show me",
|
| 126 |
-
]
|
| 127 |
-
message_lower = message.lower()
|
| 128 |
-
return any(trigger in message_lower for trigger in image_triggers)
|
| 129 |
-
|
| 130 |
-
def generate_image(prompt):
|
| 131 |
-
"""Generate image using HuggingFace inference API"""
|
| 132 |
try:
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
except Exception as e:
|
| 137 |
-
print(f"Image generation error: {e}")
|
| 138 |
-
return None
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
try:
|
| 162 |
-
return transliterate(text, sanscript.ITRANS, sanscript.BENGALI)
|
| 163 |
-
except:
|
| 164 |
-
return text
|
| 165 |
-
|
| 166 |
-
return text_lower
|
| 167 |
-
|
| 168 |
-
|
| 169 |
|
| 170 |
def create_chat_interface():
|
| 171 |
# Custom CSS for better styling with Inter font and animations
|
|
|
|
| 27 |
API_URL = "https://api-inference.huggingface.co/models/SG161222/RealVisXL_V4.0"
|
| 28 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 29 |
|
| 30 |
+
def detect_language(text):
|
| 31 |
+
"""Detect the language of input text"""
|
|
|
|
| 32 |
try:
|
| 33 |
+
return langdetect.detect(text)
|
| 34 |
+
except:
|
| 35 |
+
return 'en'
|
| 36 |
+
|
| 37 |
+
def detect_and_transliterate(text):
|
| 38 |
+
"""Detect script and transliterate if needed"""
|
| 39 |
+
try:
|
| 40 |
+
script = detect_script(text)
|
| 41 |
+
if script:
|
| 42 |
+
# Transliterate to Latin script if the text is in another script
|
| 43 |
+
return transliterate(text, script, sanscript.IAST)
|
|
|
|
|
|
|
|
|
|
| 44 |
except:
|
| 45 |
+
pass
|
| 46 |
+
return text
|
| 47 |
|
| 48 |
+
def translate_to_english(text):
|
| 49 |
+
"""Translate text to English if not already in English"""
|
| 50 |
+
try:
|
| 51 |
+
lang = detect_language(text)
|
| 52 |
+
if lang != 'en':
|
| 53 |
+
translator = GoogleTranslator(source=lang, target='en')
|
| 54 |
+
return translator.translate(text)
|
| 55 |
+
except:
|
| 56 |
+
pass
|
| 57 |
+
return text
|
| 58 |
+
|
| 59 |
+
def check_for_image_generation(message):
|
| 60 |
+
"""Check if message contains image generation commands"""
|
| 61 |
+
image_triggers = [
|
| 62 |
+
r"generate (?:an? )?image",
|
| 63 |
+
r"create (?:an? )?image",
|
| 64 |
+
r"draw",
|
| 65 |
+
r"show me",
|
| 66 |
+
r"visualize",
|
| 67 |
+
r"make (?:an? )?picture"
|
| 68 |
]
|
| 69 |
|
| 70 |
+
for trigger in image_triggers:
|
| 71 |
+
if re.search(trigger, message.lower()):
|
| 72 |
+
return True
|
| 73 |
+
return False
|
| 74 |
|
| 75 |
+
def user_message(message, history):
|
| 76 |
+
"""Handle user messages"""
|
| 77 |
+
if message is None or message == "":
|
| 78 |
+
return "", history
|
|
|
|
| 79 |
|
| 80 |
+
# Add user message to history
|
| 81 |
+
history = history + [(message, None)]
|
| 82 |
+
return "", history
|
| 83 |
+
|
| 84 |
+
def bot_response(history, system_msg, max_tokens, temperature, top_p):
|
| 85 |
+
"""Generate bot response"""
|
| 86 |
+
if not history:
|
| 87 |
+
return history, None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
last_user_message = history[-1][0]
|
|
|
|
|
|
|
|
|
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
try:
|
| 92 |
+
# Process the message for language and script
|
| 93 |
+
processed_message = detect_and_transliterate(last_user_message)
|
| 94 |
+
translated_message = translate_to_english(processed_message)
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
# Check for image generation request
|
| 97 |
+
if check_for_image_generation(translated_message):
|
| 98 |
+
# Generate image based on the message
|
| 99 |
+
image = generate_image(translated_message)
|
| 100 |
+
# Update history with a confirmation message
|
| 101 |
+
history[-1] = (history[-1][0], "I've generated an image based on your request.")
|
| 102 |
+
return history, image
|
| 103 |
+
|
| 104 |
+
# Prepare chat messages for the model
|
| 105 |
+
messages = [
|
| 106 |
+
{"role": "system", "content": system_msg},
|
| 107 |
+
]
|
| 108 |
+
|
| 109 |
+
# Add conversation history
|
| 110 |
+
for msg in history[:-1]: # Exclude the last message pair
|
| 111 |
+
if msg[0]: # User message
|
| 112 |
+
messages.append({"role": "user", "content": msg[0]})
|
| 113 |
+
if msg[1]: # Assistant message
|
| 114 |
+
messages.append({"role": "assistant", "content": msg[1]})
|
| 115 |
+
|
| 116 |
+
# Add the last user message
|
| 117 |
+
messages.append({"role": "user", "content": translated_message})
|
| 118 |
+
|
| 119 |
+
# Get model response
|
| 120 |
+
response = text_client.text_generation(
|
| 121 |
+
prompt=str(messages),
|
| 122 |
+
max_new_tokens=max_tokens,
|
| 123 |
+
temperature=temperature,
|
| 124 |
+
top_p=top_p,
|
| 125 |
+
repetition_penalty=1.1,
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Update history with bot response
|
| 129 |
+
history[-1] = (history[-1][0], response)
|
| 130 |
+
|
| 131 |
+
except Exception as e:
|
| 132 |
+
# Handle errors gracefully
|
| 133 |
+
error_message = f"I apologize, but I encountered an error: {str(e)}"
|
| 134 |
+
history[-1] = (history[-1][0], error_message)
|
| 135 |
|
| 136 |
+
return history, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
def create_chat_interface():
|
| 139 |
# Custom CSS for better styling with Inter font and animations
|