pvanand commited on
Commit
f164260
1 Parent(s): f61fcd6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -14
main.py CHANGED
@@ -132,19 +132,24 @@ def update_db(user_id, conversation_id, message, response):
132
  # Utility Functions
133
  # ============================================================================
134
 
135
- def extract_data_from_tag(input_string, tag):
136
  # Create the regex pattern
137
  pattern = f'<{tag}.*?>(.*?)</{tag}>'
138
 
139
  # Find all matches
140
  matches = re.findall(pattern, input_string, re.DOTALL)
141
 
142
- # If matches are found, return them joined by newlines
143
- if matches:
144
- out = '\n'.join(match.strip() for match in matches)
145
- return out
146
  else:
147
- return input_string
 
 
 
 
 
148
 
149
  def calculate_tokens(msgs):
150
  return sum(len(encoding.encode(str(m))) for m in msgs)
@@ -409,6 +414,9 @@ async def presentation_chat(query: PresentationChatModel, background_tasks: Back
409
 
410
  # Collect the entire response
411
  full_response = ""
 
 
 
412
  async for chunk in response_stream.body_iterator:
413
  full_response += chunk
414
 
@@ -416,16 +424,18 @@ async def presentation_chat(query: PresentationChatModel, background_tasks: Back
416
  # Extract the Marp presentation content
417
  marp_content = extract_data_from_tag(full_response, "marp_presentation")
418
 
419
- # Replace image keywords
420
- marp_content_with_images = replace_image_keywords(marp_content)
 
 
 
 
421
 
422
- # Convert Markdown to HTML
423
- html_content = convert_markdown_marp(marp_content_with_images, 'html')
424
 
425
- if html_content:
426
- return JSONResponse({
427
- "markdown": marp_content_with_images,
428
- "html": html_content.decode()
429
  })
430
  else:
431
  raise HTTPException(status_code=500, detail="Failed to create presentation.")
 
132
  # Utility Functions
133
  # ============================================================================
134
 
135
+ def extract_data_from_tag(input_string, tag, invert=False):
136
  # Create the regex pattern
137
  pattern = f'<{tag}.*?>(.*?)</{tag}>'
138
 
139
  # Find all matches
140
  matches = re.findall(pattern, input_string, re.DOTALL)
141
 
142
+ if invert:
143
+ # If inverted, replace all matches with empty string
144
+ out = re.sub(pattern, '', input_string, flags=re.DOTALL)
145
+ return out.strip()
146
  else:
147
+ # If matches are found, return them joined by newlines
148
+ if matches:
149
+ out = '\n'.join(match.strip() for match in matches)
150
+ return out
151
+ else:
152
+ return input_string
153
 
154
  def calculate_tokens(msgs):
155
  return sum(len(encoding.encode(str(m))) for m in msgs)
 
414
 
415
  # Collect the entire response
416
  full_response = ""
417
+ html_content = ""
418
+ marp_content_with_images = ""
419
+
420
  async for chunk in response_stream.body_iterator:
421
  full_response += chunk
422
 
 
424
  # Extract the Marp presentation content
425
  marp_content = extract_data_from_tag(full_response, "marp_presentation")
426
 
427
+ if marp_content:
428
+ # Replace image keywords
429
+ marp_content_with_images = replace_image_keywords(marp_content)
430
+
431
+ # Convert Markdown to HTML
432
+ html_content = convert_markdown_marp(marp_content_with_images, 'html')
433
 
 
 
434
 
435
+ return JSONResponse({
436
+ "response": extract_data_from_tag(full_response, "marp_presentation",invert=True),
437
+ "markdown_presentation": marp_content_with_images,
438
+ "html_presentation": html_content.decode() if isinstance(html_content, bytes) else html_content
439
  })
440
  else:
441
  raise HTTPException(status_code=500, detail="Failed to create presentation.")