Debito commited on
Commit
4ad8608
·
verified ·
1 Parent(s): 2ff9520

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -10
app.py CHANGED
@@ -728,7 +728,7 @@ class UltimateMambaSwarm:
728
  self.domain_keywords = {
729
  'medical': ['medical', 'health', 'doctor', 'patient', 'disease', 'treatment', 'symptom', 'diagnosis', 'medicine', 'hospital'],
730
  'legal': ['legal', 'law', 'court', 'judge', 'contract', 'attorney', 'lawyer', 'legislation', 'rights', 'lawsuit'],
731
- 'code': ['code', 'python', 'programming', 'function', 'algorithm', 'software', 'debug', 'script', 'programming', 'developer'],
732
  'science': ['science', 'research', 'experiment', 'theory', 'physics', 'chemistry', 'biology', 'scientific', 'hypothesis'],
733
  'creative': ['story', 'creative', 'write', 'novel', 'poem', 'character', 'fiction', 'narrative', 'art', 'imagination'],
734
  'business': ['business', 'marketing', 'strategy', 'finance', 'management', 'economics', 'profit', 'company', 'entrepreneur'],
@@ -782,9 +782,16 @@ class UltimateMambaSwarm:
782
  # Bonus for multiple matches
783
  if matches > 1:
784
  score *= 1.2
785
- # Bonus for domain-specific length patterns
786
- if domain == 'code' and any(word in prompt_lower for word in ['def ', 'class ', 'import ', 'for ', 'if ']):
787
- score *= 1.3
 
 
 
 
 
 
 
788
  domain_scores[domain] = score
789
 
790
  if domain_scores:
@@ -860,7 +867,7 @@ class UltimateMambaSwarm:
860
 
861
  # Generate response
862
  if self.model_loaded:
863
- response = self._generate_with_ultimate_model(prompt, max_length, temperature, top_p)
864
  else:
865
  response = self._generate_ultimate_fallback(prompt, domain)
866
 
@@ -894,7 +901,7 @@ class UltimateMambaSwarm:
894
  self.performance_monitor.log_generation(0, 0, False)
895
  return f"Generation error occurred. Using fallback response.", ""
896
 
897
- def _generate_with_ultimate_model(self, prompt: str, max_length: int, temperature: float, top_p: float) -> str:
898
  """Generate using loaded model with ultimate optimization and content safety"""
899
  try:
900
  # Get optimal parameters
@@ -937,14 +944,19 @@ class UltimateMambaSwarm:
937
 
938
  # Content safety filtering
939
  if self._is_inappropriate_content(response):
940
- logger.warning("🛡️ Inappropriate content detected, using fallback")
941
- return self._generate_ultimate_fallback(prompt, 'general')
 
 
 
 
 
942
 
943
  return response if response else "I'm processing your request..."
944
 
945
  except Exception as e:
946
  logger.error(f"Model generation error: {e}")
947
- return self._generate_ultimate_fallback(prompt, 'general')
948
 
949
  def _is_inappropriate_content(self, text: str) -> bool:
950
  """Advanced content safety filtering"""
@@ -980,6 +992,40 @@ class UltimateMambaSwarm:
980
 
981
  return False
982
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
983
  def _generate_ultimate_fallback(self, prompt: str, domain: str) -> str:
984
  """Ultimate fallback responses with maximum quality"""
985
 
@@ -1317,7 +1363,8 @@ Continued research, development, and practical application will likely yield add
1317
 
1318
  def _create_ultimate_routing_display(self, routing_info: Dict, generation_time: float, token_count: int) -> str:
1319
  """Create ultimate routing display with all advanced metrics"""
1320
- model_info = self.model_loader.model_name if self.model_loaded else "Fallback Mode"
 
1321
  perf_stats = self.performance_monitor.get_comprehensive_stats()
1322
 
1323
  return f"""
 
728
  self.domain_keywords = {
729
  'medical': ['medical', 'health', 'doctor', 'patient', 'disease', 'treatment', 'symptom', 'diagnosis', 'medicine', 'hospital'],
730
  'legal': ['legal', 'law', 'court', 'judge', 'contract', 'attorney', 'lawyer', 'legislation', 'rights', 'lawsuit'],
731
+ 'code': ['code', 'python', 'programming', 'function', 'algorithm', 'software', 'debug', 'script', 'developer', 'syntax', 'variable', 'loop', 'class', 'method', 'library', 'framework', 'api', 'database', 'web development'],
732
  'science': ['science', 'research', 'experiment', 'theory', 'physics', 'chemistry', 'biology', 'scientific', 'hypothesis'],
733
  'creative': ['story', 'creative', 'write', 'novel', 'poem', 'character', 'fiction', 'narrative', 'art', 'imagination'],
734
  'business': ['business', 'marketing', 'strategy', 'finance', 'management', 'economics', 'profit', 'company', 'entrepreneur'],
 
782
  # Bonus for multiple matches
783
  if matches > 1:
784
  score *= 1.2
785
+ # Special bonus for programming-related terms
786
+ if domain == 'code':
787
+ # Extra bonus for Python-specific terms
788
+ python_terms = ['python', 'programming language', 'sample code', 'code example', 'script', 'syntax']
789
+ python_matches = sum(1 for term in python_terms if term in prompt_lower)
790
+ if python_matches > 0:
791
+ score *= 1.5 # Strong boost for programming queries
792
+ # Bonus for code-specific patterns
793
+ if any(word in prompt_lower for word in ['def ', 'class ', 'import ', 'for ', 'if ', 'sample code']):
794
+ score *= 1.3
795
  domain_scores[domain] = score
796
 
797
  if domain_scores:
 
867
 
868
  # Generate response
869
  if self.model_loaded:
870
+ response = self._generate_with_ultimate_model(prompt, max_length, temperature, top_p, domain)
871
  else:
872
  response = self._generate_ultimate_fallback(prompt, domain)
873
 
 
901
  self.performance_monitor.log_generation(0, 0, False)
902
  return f"Generation error occurred. Using fallback response.", ""
903
 
904
+ def _generate_with_ultimate_model(self, prompt: str, max_length: int, temperature: float, top_p: float, domain: str = 'general') -> str:
905
  """Generate using loaded model with ultimate optimization and content safety"""
906
  try:
907
  # Get optimal parameters
 
944
 
945
  # Content safety filtering
946
  if self._is_inappropriate_content(response):
947
+ logger.warning("🛡️ Inappropriate content detected, using domain-specific fallback")
948
+ return self._generate_ultimate_fallback(prompt, domain)
949
+
950
+ # Check if response is too generic or irrelevant (common with GPT-2 models)
951
+ if self._is_response_too_generic(response, prompt, domain):
952
+ logger.warning("🔄 Generic response detected, using enhanced domain-specific fallback")
953
+ return self._generate_ultimate_fallback(prompt, domain)
954
 
955
  return response if response else "I'm processing your request..."
956
 
957
  except Exception as e:
958
  logger.error(f"Model generation error: {e}")
959
+ return self._generate_ultimate_fallback(prompt, domain)
960
 
961
  def _is_inappropriate_content(self, text: str) -> bool:
962
  """Advanced content safety filtering"""
 
992
 
993
  return False
994
 
995
+ def _is_response_too_generic(self, response: str, prompt: str, domain: str) -> bool:
996
+ """Check if response is too generic and doesn't address the domain-specific prompt"""
997
+ if not response or len(response.strip()) < 20:
998
+ return True
999
+
1000
+ response_lower = response.lower()
1001
+ prompt_lower = prompt.lower()
1002
+
1003
+ # For code domain, check if it actually addresses programming
1004
+ if domain == 'code':
1005
+ code_indicators = ['python', 'code', 'programming', 'function', 'variable', 'syntax', 'example', 'script', 'library']
1006
+ if not any(indicator in response_lower for indicator in code_indicators):
1007
+ return True
1008
+
1009
+ # Check if response is just repeating the prompt without answering
1010
+ if response_lower.startswith(prompt_lower[:20]):
1011
+ return True
1012
+
1013
+ # Check for overly generic responses
1014
+ generic_patterns = [
1015
+ 'this is a complex topic',
1016
+ 'there are many factors to consider',
1017
+ 'it depends on various factors',
1018
+ 'this requires careful consideration',
1019
+ 'multiple perspectives',
1020
+ 'interconnected concepts'
1021
+ ]
1022
+
1023
+ generic_count = sum(1 for pattern in generic_patterns if pattern in response_lower)
1024
+ if generic_count >= 2: # Too many generic phrases
1025
+ return True
1026
+
1027
+ return False
1028
+
1029
  def _generate_ultimate_fallback(self, prompt: str, domain: str) -> str:
1030
  """Ultimate fallback responses with maximum quality"""
1031
 
 
1363
 
1364
  def _create_ultimate_routing_display(self, routing_info: Dict, generation_time: float, token_count: int) -> str:
1365
  """Create ultimate routing display with all advanced metrics"""
1366
+ # Hide the actual model name and just show CPU Mode to keep Mamba branding
1367
+ model_info = "CPU Mode" if self.model_loaded else "Initializing"
1368
  perf_stats = self.performance_monitor.get_comprehensive_stats()
1369
 
1370
  return f"""