Reality123b commited on
Commit
5ac6df3
·
verified ·
1 Parent(s): 69bd0b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -36
app.py CHANGED
@@ -7,21 +7,22 @@ from indic_transliteration.sanscript import transliterate
7
  import langdetect
8
  import re
9
 
10
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
11
 
12
  def detect_language_script(text: str) -> tuple[str, str]:
13
- """
14
- Detect language and script of the input text.
15
- Returns (language_code, script_type)
16
- """
17
  try:
18
  # Use confidence threshold to avoid false detections
19
  lang_detect = langdetect.detect_langs(text)
20
- if lang_detect[0].prob > 0.8: # Only accept high confidence detections
 
21
  lang = lang_detect[0].lang
22
  else:
23
  lang = 'en' # Default to English if unsure
24
-
25
  script = None
26
  try:
27
  script = detect_script(text)
@@ -32,10 +33,8 @@ def detect_language_script(text: str) -> tuple[str, str]:
32
  return 'en', None
33
 
34
  def is_romanized_indic(text: str) -> bool:
35
- """
36
- Check if text appears to be romanized Indic language.
37
- More strict pattern matching.
38
- """
39
  # Common Bengali romanized patterns with word boundaries
40
  bengali_patterns = [
41
  r'\b(ami|tumi|apni)\b', # Common pronouns
@@ -50,13 +49,11 @@ def is_romanized_indic(text: str) -> bool:
50
  return matches >= 2 # Require at least 2 matches to consider it Bengali
51
 
52
  def translate_text(text: str, target_lang='en') -> tuple[str, str, bool]:
53
- """
54
- Translate text to target language, with more conservative translation logic.
55
- """
56
  # Skip translation for very short inputs or basic greetings
57
  if len(text.split()) <= 2 or text.lower() in ['hello', 'hi', 'hey']:
58
  return text, 'en', False
59
-
60
  original_lang, script = detect_language_script(text)
61
  is_transliterated = False
62
 
@@ -69,12 +66,12 @@ def translate_text(text: str, target_lang='en') -> tuple[str, str, bool]:
69
  except Exception as e:
70
  print(f"Translation error: {e}")
71
  return text, 'en', False
72
-
73
  # Check for romanized Indic text only if it's a longer input
74
  if original_lang == 'en' and len(text.split()) > 2 and is_romanized_indic(text):
75
  text = romanized_to_bengali(text)
76
  return translate_text(text, target_lang) # Recursive call with Bengali script
77
-
78
  return text, 'en', False
79
 
80
  def check_custom_responses(message: str) -> str:
@@ -91,12 +88,47 @@ def check_custom_responses(message: str) -> str:
91
  "who is ur dev": "sk md saad amin",
92
  "who is ur developer": "sk md saad amin",
93
  }
94
-
95
  for pattern, response in custom_responses.items():
96
  if pattern in message_lower:
97
  return response
98
  return None
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  def romanized_to_bengali(text: str) -> str:
101
  """Convert romanized Bengali text to Bengali script."""
102
  bengali_mappings = {
@@ -126,11 +158,11 @@ def romanized_to_bengali(text: str) -> str:
126
  return text_lower
127
 
128
  def respond(
129
- message,
130
- history: list[tuple[str, str]],
131
- system_message,
132
- max_tokens,
133
- temperature,
134
  top_p,
135
  ):
136
  # First check for custom responses
@@ -139,9 +171,25 @@ def respond(
139
  yield custom_response
140
  return
141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  # Handle translation with more conservative approach
143
  translated_msg, original_lang, was_transliterated = translate_text(message)
144
-
145
  # Prepare conversation history - only translate if necessary
146
  messages = [{"role": "system", "content": system_message}]
147
  for val in history:
@@ -156,10 +204,10 @@ def respond(
156
  messages.append({"role": "assistant", "content": val[1]})
157
 
158
  messages.append({"role": "user", "content": translated_msg})
159
-
160
  # Get response from model
161
  response = ""
162
- for message in client.chat_completion(
163
  messages,
164
  max_tokens=max_tokens,
165
  stream=True,
@@ -168,18 +216,19 @@ def respond(
168
  ):
169
  token = message.choices[0].delta.content
170
  response += token
171
-
172
- # Only translate back if the original was definitely non-English
173
- if original_lang != 'en' and len(message.split()) > 2:
174
- try:
175
- translator = GoogleTranslator(source='en', target=original_lang)
176
- translated_response = translator.translate(response)
177
- yield translated_response
178
- except:
179
- yield response
180
- else:
181
  yield response
 
 
182
 
 
183
  demo = gr.ChatInterface(
184
  respond,
185
  additional_inputs=[
 
7
  import langdetect
8
  import re
9
 
10
+ # Initialize clients
11
+ text_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
+ image_client = InferenceClient("ijohn07/DALLE-4K")
13
 
14
  def detect_language_script(text: str) -> tuple[str, str]:
15
+ """Detect language and script of the input text.
16
+ Returns (language_code, script_type)"""
 
 
17
  try:
18
  # Use confidence threshold to avoid false detections
19
  lang_detect = langdetect.detect_langs(text)
20
+ if lang_detect[0].prob > 0.8:
21
+ # Only accept high confidence detections
22
  lang = lang_detect[0].lang
23
  else:
24
  lang = 'en' # Default to English if unsure
25
+
26
  script = None
27
  try:
28
  script = detect_script(text)
 
33
  return 'en', None
34
 
35
  def is_romanized_indic(text: str) -> bool:
36
+ """Check if text appears to be romanized Indic language.
37
+ More strict pattern matching."""
 
 
38
  # Common Bengali romanized patterns with word boundaries
39
  bengali_patterns = [
40
  r'\b(ami|tumi|apni)\b', # Common pronouns
 
49
  return matches >= 2 # Require at least 2 matches to consider it Bengali
50
 
51
  def translate_text(text: str, target_lang='en') -> tuple[str, str, bool]:
52
+ """Translate text to target language, with more conservative translation logic."""
 
 
53
  # Skip translation for very short inputs or basic greetings
54
  if len(text.split()) <= 2 or text.lower() in ['hello', 'hi', 'hey']:
55
  return text, 'en', False
56
+
57
  original_lang, script = detect_language_script(text)
58
  is_transliterated = False
59
 
 
66
  except Exception as e:
67
  print(f"Translation error: {e}")
68
  return text, 'en', False
69
+
70
  # Check for romanized Indic text only if it's a longer input
71
  if original_lang == 'en' and len(text.split()) > 2 and is_romanized_indic(text):
72
  text = romanized_to_bengali(text)
73
  return translate_text(text, target_lang) # Recursive call with Bengali script
74
+
75
  return text, 'en', False
76
 
77
  def check_custom_responses(message: str) -> str:
 
88
  "who is ur dev": "sk md saad amin",
89
  "who is ur developer": "sk md saad amin",
90
  }
 
91
  for pattern, response in custom_responses.items():
92
  if pattern in message_lower:
93
  return response
94
  return None
95
 
96
+ def is_image_request(message: str) -> bool:
97
+ """Detect if the message is requesting image generation."""
98
+ image_triggers = [
99
+ "generate an image",
100
+ "create an image",
101
+ "draw",
102
+ "make a picture",
103
+ "generate a picture",
104
+ "create a picture",
105
+ "generate art",
106
+ "create art",
107
+ "make art",
108
+ "visualize",
109
+ "show me",
110
+ ]
111
+ message_lower = message.lower()
112
+ return any(trigger in message_lower for trigger in image_triggers)
113
+
114
+ def generate_image(prompt: str) -> str:
115
+ """Generate an image using DALLE-4K model."""
116
+ try:
117
+ response = image_client.text_to_image(
118
+ prompt,
119
+ parameters={
120
+ "negative_prompt": "blurry, bad quality, nsfw",
121
+ "num_inference_steps": 30,
122
+ "guidance_scale": 7.5
123
+ }
124
+ )
125
+ # Save the image and return the path or base64 string
126
+ # Note: Implementation depends on how you want to handle the image output
127
+ return response
128
+ except Exception as e:
129
+ print(f"Image generation error: {e}")
130
+ return None
131
+
132
  def romanized_to_bengali(text: str) -> str:
133
  """Convert romanized Bengali text to Bengali script."""
134
  bengali_mappings = {
 
158
  return text_lower
159
 
160
  def respond(
161
+ message,
162
+ history: list[tuple[str, str]],
163
+ system_message,
164
+ max_tokens,
165
+ temperature,
166
  top_p,
167
  ):
168
  # First check for custom responses
 
171
  yield custom_response
172
  return
173
 
174
+ # Check if this is an image generation request
175
+ if is_image_request(message):
176
+ try:
177
+ image = generate_image(message)
178
+ if image:
179
+ yield f"Here's your generated image based on: {message}"
180
+ # You'll need to implement the actual image display logic
181
+ # depending on your Gradio interface requirements
182
+ return
183
+ else:
184
+ yield "Sorry, I couldn't generate the image. Please try again."
185
+ return
186
+ except Exception as e:
187
+ yield f"An error occurred while generating the image: {str(e)}"
188
+ return
189
+
190
  # Handle translation with more conservative approach
191
  translated_msg, original_lang, was_transliterated = translate_text(message)
192
+
193
  # Prepare conversation history - only translate if necessary
194
  messages = [{"role": "system", "content": system_message}]
195
  for val in history:
 
204
  messages.append({"role": "assistant", "content": val[1]})
205
 
206
  messages.append({"role": "user", "content": translated_msg})
207
+
208
  # Get response from model
209
  response = ""
210
+ for message in text_client.chat_completion(
211
  messages,
212
  max_tokens=max_tokens,
213
  stream=True,
 
216
  ):
217
  token = message.choices[0].delta.content
218
  response += token
219
+
220
+ # Only translate back if the original was definitely non-English
221
+ if original_lang != 'en' and len(message.split()) > 2:
222
+ try:
223
+ translator = GoogleTranslator(source='en', target=original_lang)
224
+ translated_response = translator.translate(response)
225
+ yield translated_response
226
+ except:
 
 
227
  yield response
228
+ else:
229
+ yield response
230
 
231
+ # Updated Gradio interface to handle images
232
  demo = gr.ChatInterface(
233
  respond,
234
  additional_inputs=[