Update main.py
Browse files
main.py
CHANGED
@@ -39,11 +39,31 @@ rate_limit_store = defaultdict(lambda: {"count": 0, "timestamp": time.time()})
|
|
39 |
CLEANUP_INTERVAL = 60 # seconds
|
40 |
RATE_LIMIT_WINDOW = 60 # seconds
|
41 |
|
42 |
-
# Define
|
43 |
class ImageResponseModel(BaseModel):
|
44 |
images: str
|
45 |
alt: str
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# Updated Blackbox Class
|
48 |
class Blackbox:
|
49 |
label = "Blackbox AI"
|
@@ -206,6 +226,8 @@ class Blackbox:
|
|
206 |
def clean_response(text: str) -> str:
|
207 |
pattern = r'^\$\@\$v=undefined-rv1\$\@\$'
|
208 |
cleaned_text = re.sub(pattern, '', text)
|
|
|
|
|
209 |
return cleaned_text
|
210 |
|
211 |
@classmethod
|
@@ -633,9 +655,14 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
|
|
633 |
websearch=False # Modify if websearch is needed
|
634 |
):
|
635 |
if isinstance(chunk, ImageResponseModel):
|
|
|
636 |
yield f"Image URL: {chunk.images}\n"
|
637 |
else:
|
638 |
-
yield
|
|
|
|
|
|
|
|
|
639 |
|
640 |
logger.info(f"Initiating streaming response for API key: {api_key} | IP: {client_ip}")
|
641 |
return StreamingResponse(content_generator(), media_type='text/plain')
|
|
|
39 |
CLEANUP_INTERVAL = 60 # seconds
|
40 |
RATE_LIMIT_WINDOW = 60 # seconds
|
41 |
|
42 |
+
# Define ImageResponseModel
|
43 |
class ImageResponseModel(BaseModel):
|
44 |
images: str
|
45 |
alt: str
|
46 |
|
47 |
+
def strip_markdown(text: str) -> str:
|
48 |
+
"""
|
49 |
+
Strips markdown syntax from the given text to ensure plain text.
|
50 |
+
"""
|
51 |
+
# Remove bold (**text** or __text__)
|
52 |
+
text = re.sub(r'(\*\*|__)(.*?)\1', r'\2', text)
|
53 |
+
# Remove italic (*text* or _text_)
|
54 |
+
text = re.sub(r'(\*|_)(.*?)\1', r'\2', text)
|
55 |
+
# Remove inline code (`code`)
|
56 |
+
text = re.sub(r'`(.*?)`', r'\1', text)
|
57 |
+
# Remove links [text](url)
|
58 |
+
text = re.sub(r'\[(.*?)\]\((.*?)\)', r'\1', text)
|
59 |
+
# Remove images 
|
60 |
+
text = re.sub(r'!\[(.*?)\]\((.*?)\)', r'\1', text)
|
61 |
+
# Remove headers (# Header)
|
62 |
+
text = re.sub(r'#+\s+(.*)', r'\1', text)
|
63 |
+
# Remove any remaining markdown characters
|
64 |
+
text = re.sub(r'[*_`>#]', '', text)
|
65 |
+
return text
|
66 |
+
|
67 |
# Updated Blackbox Class
|
68 |
class Blackbox:
|
69 |
label = "Blackbox AI"
|
|
|
226 |
def clean_response(text: str) -> str:
|
227 |
pattern = r'^\$\@\$v=undefined-rv1\$\@\$'
|
228 |
cleaned_text = re.sub(pattern, '', text)
|
229 |
+
# Strip markdown syntax to prevent bold text
|
230 |
+
cleaned_text = strip_markdown(cleaned_text)
|
231 |
return cleaned_text
|
232 |
|
233 |
@classmethod
|
|
|
655 |
websearch=False # Modify if websearch is needed
|
656 |
):
|
657 |
if isinstance(chunk, ImageResponseModel):
|
658 |
+
# Yield image URLs as plain text
|
659 |
yield f"Image URL: {chunk.images}\n"
|
660 |
else:
|
661 |
+
# Ensure chunk is a string and yield as plain text
|
662 |
+
if isinstance(chunk, str):
|
663 |
+
yield f"{chunk}\n"
|
664 |
+
else:
|
665 |
+
yield f"{str(chunk)}\n"
|
666 |
|
667 |
logger.info(f"Initiating streaming response for API key: {api_key} | IP: {client_ip}")
|
668 |
return StreamingResponse(content_generator(), media_type='text/plain')
|