Spaces:
Running
Running
Update main.py
Browse files
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 ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|