codelion commited on
Commit
bd57608
Β·
verified Β·
1 Parent(s): 4b01151

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +28 -0
main.py CHANGED
@@ -54,6 +54,32 @@ model = "HuggingFaceTB/SmolLM3-3B"
54
  temperature = 0.1
55
  max_tokens = 500
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  def response_generator(query: str) -> str:
58
  """Ask the RAG chain to answer `query`, with JSON‑error fallback."""
59
  # log usage
@@ -109,6 +135,8 @@ def response_generator(query: str) -> str:
109
  "I’m sorry, I don’t have enough information to answer that. "
110
  "If you have a public data source to add, please email [email protected]."
111
  )
 
 
112
  return answer
113
 
114
  # ─────── Streamlit UI ──────────────────────────────────────────────────────────
 
54
  temperature = 0.1
55
  max_tokens = 500
56
 
57
+ import re
58
+
59
+ def clean_response(answer: str) -> str:
60
+ """Clean up AI response by removing unwanted artifacts and formatting."""
61
+ if not answer:
62
+ return answer
63
+
64
+ # Remove thinking tags and content
65
+ answer = re.sub(r'<think>.*?</think>', '', answer, flags=re.DOTALL)
66
+ answer = re.sub(r'<thinking>.*?</thinking>', '', answer, flags=re.DOTALL)
67
+
68
+ # Remove other common AI response artifacts
69
+ answer = re.sub(r'\[.*?\]', '', answer, flags=re.DOTALL) # Remove bracketed content
70
+ answer = re.sub(r'\{.*?\}', '', answer, flags=re.DOTALL) # Remove curly bracketed content
71
+ answer = re.sub(r'```.*?```', '', answer, flags=re.DOTALL) # Remove code blocks
72
+ answer = re.sub(r'---.*?---', '', answer, flags=re.DOTALL) # Remove dashed sections
73
+
74
+ # Remove excessive whitespace and newlines
75
+ answer = re.sub(r'\s+', ' ', answer).strip()
76
+
77
+ # Remove common AI-generated prefixes/suffixes
78
+ answer = re.sub(r'^(Assistant:|AI:|Grok:)\s*', '', answer, flags=re.IGNORECASE)
79
+ answer = re.sub(r'\s*(Sincerely,.*|Best regards,.*|Regards,.*)$', '', answer, flags=re.IGNORECASE)
80
+
81
+ return answer
82
+
83
  def response_generator(query: str) -> str:
84
  """Ask the RAG chain to answer `query`, with JSON‑error fallback."""
85
  # log usage
 
135
  "I’m sorry, I don’t have enough information to answer that. "
136
  "If you have a public data source to add, please email [email protected]."
137
  )
138
+
139
+ answer = clean_response(answer)
140
  return answer
141
 
142
  # ─────── Streamlit UI ──────────────────────────────────────────────────────────