Update app.py
Browse files
app.py
CHANGED
|
@@ -6,30 +6,36 @@ import docx
|
|
| 6 |
import re
|
| 7 |
|
| 8 |
def remove_links_keep_issue_numbers(text):
|
| 9 |
-
# Remove links but keep issue numbers
|
| 10 |
return re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def markdown_to_docx(doc, markdown_text):
|
| 13 |
-
# Split the markdown into lines
|
| 14 |
lines = markdown_text.split('\n')
|
| 15 |
|
| 16 |
for line in lines:
|
| 17 |
-
|
| 18 |
if line.startswith('# '):
|
| 19 |
-
doc.add_heading(
|
| 20 |
elif line.startswith('## '):
|
| 21 |
-
doc.add_heading(
|
| 22 |
elif line.startswith('### '):
|
| 23 |
-
doc.add_heading(
|
| 24 |
-
# Check for list items
|
| 25 |
elif line.strip().startswith('- ') or line.strip().startswith('* '):
|
| 26 |
-
doc.add_paragraph(
|
| 27 |
-
# Check for numbered list items
|
| 28 |
elif re.match(r'^\d+\.', line.strip()):
|
| 29 |
-
doc.add_paragraph(
|
| 30 |
-
# Regular paragraph
|
| 31 |
else:
|
| 32 |
-
doc.add_paragraph(
|
| 33 |
|
| 34 |
def generate_release_notes(github_url, github_token, gemini_api_key, start_date, end_date):
|
| 35 |
try:
|
|
@@ -63,17 +69,13 @@ def generate_release_notes(github_url, github_token, gemini_api_key, start_date,
|
|
| 63 |
|
| 64 |
response = model.generate_content(prompt)
|
| 65 |
|
| 66 |
-
# Remove links but keep issue numbers
|
| 67 |
release_notes = remove_links_keep_issue_numbers(response.text)
|
| 68 |
|
| 69 |
-
# Create a Word document
|
| 70 |
doc = docx.Document()
|
| 71 |
doc.add_heading('Release Notes', 0)
|
| 72 |
|
| 73 |
-
# Convert markdown to docx
|
| 74 |
markdown_to_docx(doc, release_notes)
|
| 75 |
|
| 76 |
-
# Save the document to a temporary file
|
| 77 |
temp_file = "release_notes.docx"
|
| 78 |
doc.save(temp_file)
|
| 79 |
|
|
@@ -89,11 +91,9 @@ def generate_release_notes(github_url, github_token, gemini_api_key, start_date,
|
|
| 89 |
except Exception as e:
|
| 90 |
return f"An error occurred: {str(e)}", None
|
| 91 |
|
| 92 |
-
# Set default dates
|
| 93 |
default_end_date = datetime.now()
|
| 94 |
-
default_start_date = default_end_date - timedelta(days=30)
|
| 95 |
|
| 96 |
-
# Create Gradio interface
|
| 97 |
iface = gr.Interface(
|
| 98 |
fn=generate_release_notes,
|
| 99 |
inputs=[
|
|
@@ -122,5 +122,4 @@ iface = gr.Interface(
|
|
| 122 |
analytics_enabled=False,
|
| 123 |
)
|
| 124 |
|
| 125 |
-
# Launch the app
|
| 126 |
iface.launch()
|
|
|
|
| 6 |
import re
|
| 7 |
|
| 8 |
def remove_links_keep_issue_numbers(text):
|
|
|
|
| 9 |
return re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text)
|
| 10 |
|
| 11 |
+
def strip_markdown(text):
|
| 12 |
+
# Remove headers
|
| 13 |
+
text = re.sub(r'^#+\s*', '', text, flags=re.MULTILINE)
|
| 14 |
+
# Remove bold/italic
|
| 15 |
+
text = re.sub(r'\*{1,2}([^\*]+)\*{1,2}', r'\1', text)
|
| 16 |
+
# Remove inline code
|
| 17 |
+
text = re.sub(r'`([^`]+)`', r'\1', text)
|
| 18 |
+
# Remove blockquotes
|
| 19 |
+
text = re.sub(r'^\s*>\s*', '', text, flags=re.MULTILINE)
|
| 20 |
+
return text.strip()
|
| 21 |
+
|
| 22 |
def markdown_to_docx(doc, markdown_text):
|
|
|
|
| 23 |
lines = markdown_text.split('\n')
|
| 24 |
|
| 25 |
for line in lines:
|
| 26 |
+
stripped_line = strip_markdown(line)
|
| 27 |
if line.startswith('# '):
|
| 28 |
+
doc.add_heading(stripped_line, level=1)
|
| 29 |
elif line.startswith('## '):
|
| 30 |
+
doc.add_heading(stripped_line, level=2)
|
| 31 |
elif line.startswith('### '):
|
| 32 |
+
doc.add_heading(stripped_line, level=3)
|
|
|
|
| 33 |
elif line.strip().startswith('- ') or line.strip().startswith('* '):
|
| 34 |
+
doc.add_paragraph(stripped_line.strip()[2:], style='List Bullet')
|
|
|
|
| 35 |
elif re.match(r'^\d+\.', line.strip()):
|
| 36 |
+
doc.add_paragraph(stripped_line.strip(), style='List Number')
|
|
|
|
| 37 |
else:
|
| 38 |
+
doc.add_paragraph(stripped_line)
|
| 39 |
|
| 40 |
def generate_release_notes(github_url, github_token, gemini_api_key, start_date, end_date):
|
| 41 |
try:
|
|
|
|
| 69 |
|
| 70 |
response = model.generate_content(prompt)
|
| 71 |
|
|
|
|
| 72 |
release_notes = remove_links_keep_issue_numbers(response.text)
|
| 73 |
|
|
|
|
| 74 |
doc = docx.Document()
|
| 75 |
doc.add_heading('Release Notes', 0)
|
| 76 |
|
|
|
|
| 77 |
markdown_to_docx(doc, release_notes)
|
| 78 |
|
|
|
|
| 79 |
temp_file = "release_notes.docx"
|
| 80 |
doc.save(temp_file)
|
| 81 |
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
return f"An error occurred: {str(e)}", None
|
| 93 |
|
|
|
|
| 94 |
default_end_date = datetime.now()
|
| 95 |
+
default_start_date = default_end_date - timedelta(days=30)
|
| 96 |
|
|
|
|
| 97 |
iface = gr.Interface(
|
| 98 |
fn=generate_release_notes,
|
| 99 |
inputs=[
|
|
|
|
| 122 |
analytics_enabled=False,
|
| 123 |
)
|
| 124 |
|
|
|
|
| 125 |
iface.launch()
|