Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
from github import Github
|
| 4 |
import gitlab
|
|
@@ -7,179 +10,139 @@ import tempfile
|
|
| 7 |
import docx
|
| 8 |
import os
|
| 9 |
import logging
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Configure logging
|
| 12 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
ui_extensions = ['.erb', '.haml', '.slim', '.php', '.aspx', '.jsp', '.ftl', '.twig', '.mustache', '.handlebars', '.ejs', '.pug', '.blade.php', '.xhtml', '.fxml', '.tsx', '.jsx', '.vue', '.html', '.cshtml', '.razor', '.xaml', '.jsx']
|
| 17 |
-
return any(filename.endswith(ext) for ext in ui_extensions)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
logger.info(f"Fetching files from {git_provider} repository: {repo_url}")
|
| 22 |
-
exclude_folders = [folder.strip() for folder in exclude_folders.split(',') if folder.strip()]
|
| 23 |
-
if git_provider == "GitHub":
|
| 24 |
-
g = Github(personal_access_token)
|
| 25 |
-
repo = g.get_repo(repo_url)
|
| 26 |
-
contents = repo.get_contents("")
|
| 27 |
-
while contents:
|
| 28 |
-
file_content = contents.pop(0)
|
| 29 |
-
if file_content.type == "dir":
|
| 30 |
-
if not any(file_content.path.startswith(folder) for folder in exclude_folders):
|
| 31 |
-
contents.extend(repo.get_contents(file_content.path))
|
| 32 |
-
elif is_ui_file(file_content.name) and not any(file_content.path.startswith(folder) for folder in exclude_folders):
|
| 33 |
-
logger.info(f"Found UI file: {file_content.path}")
|
| 34 |
-
file_contents.append((file_content.path, file_content.decoded_content.decode('utf-8', errors='ignore')))
|
| 35 |
-
elif git_provider == "GitLab":
|
| 36 |
-
gl = gitlab.Gitlab(url='https://gitlab.com', private_token=personal_access_token)
|
| 37 |
-
project = gl.projects.get(repo_url)
|
| 38 |
-
items = project.repository_tree(recursive=True)
|
| 39 |
-
for item in items:
|
| 40 |
-
if item['type'] == 'blob' and is_ui_file(item['name']) and not any(item['path'].startswith(folder) for folder in exclude_folders):
|
| 41 |
-
logger.info(f"Found UI file: {item['path']}")
|
| 42 |
-
file_content = project.files.get(item['path'], ref='main')
|
| 43 |
-
file_contents.append((item['path'], file_content.decode().decode('utf-8', errors='ignore')))
|
| 44 |
-
elif git_provider == "Gitea":
|
| 45 |
-
base_url = "https://gitea.com/api/v1"
|
| 46 |
-
headers = {"Authorization": f"token {personal_access_token}"}
|
| 47 |
-
def recursive_get_contents(path=""):
|
| 48 |
-
response = requests.get(f"{base_url}/repos/{repo_url}/contents/{path}", headers=headers)
|
| 49 |
-
response.raise_for_status()
|
| 50 |
-
for item in response.json():
|
| 51 |
-
if item['type'] == 'file' and is_ui_file(item['name']) and not any(item['path'].startswith(folder) for folder in exclude_folders):
|
| 52 |
-
logger.info(f"Found UI file: {item['path']}")
|
| 53 |
-
file_content = requests.get(item['download_url']).text
|
| 54 |
-
file_contents.append((item['path'], file_content))
|
| 55 |
-
elif item['type'] == 'dir' and not any(item['path'].startswith(folder) for folder in exclude_folders):
|
| 56 |
-
recursive_get_contents(item['path'])
|
| 57 |
-
recursive_get_contents()
|
| 58 |
-
else:
|
| 59 |
-
raise ValueError("Unsupported Git provider")
|
| 60 |
-
logger.info(f"Total UI files found: {len(file_contents)}")
|
| 61 |
-
return file_contents
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
model = genai.GenerativeModel('gemini-2.0-flash-lite')
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
- Use clear section titles that has the name of the file in parenthesis
|
| 105 |
-
- Follow this numbering heirarchy (1.0, 1.1, 1.2), (2.0, 2.1, 2.2), (3.0, 3.1, 3.2)
|
| 106 |
-
- Explain the purpose and implications of each component
|
| 107 |
-
"""
|
| 108 |
|
| 109 |
-
|
| 110 |
-
logger.info(f"Generated {guide_type} section for {file_path}")
|
| 111 |
-
return response.text
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
full_guide = f"# {guide_type}\n\n" + "\n\n".join(guide_sections)
|
| 125 |
-
|
| 126 |
-
logger.info("Creating DOCX file")
|
| 127 |
-
doc = docx.Document()
|
| 128 |
-
doc.add_heading(guide_type, 0)
|
| 129 |
-
|
| 130 |
-
for line in full_guide.split('\n'):
|
| 131 |
-
line = line.strip()
|
| 132 |
-
if line.startswith('# '):
|
| 133 |
-
doc.add_heading(line[2:], level=1)
|
| 134 |
-
elif line.startswith('## '):
|
| 135 |
-
doc.add_heading(line[3:], level=2)
|
| 136 |
-
elif line.startswith('Step'):
|
| 137 |
-
doc.add_paragraph(line, style='List Number')
|
| 138 |
-
else:
|
| 139 |
-
doc.add_paragraph(line)
|
| 140 |
-
|
| 141 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as temp_docx:
|
| 142 |
-
doc.save(temp_docx.name)
|
| 143 |
-
docx_path = temp_docx.name
|
| 144 |
-
logger.info(f"DOCX file saved: {docx_path}")
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
| 157 |
-
return f"An error occurred: {str(e)}", None, None
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
label="Git Provider"
|
| 165 |
-
),
|
| 166 |
-
gr.Textbox(label="Repository URL", placeholder="owner/repo"),
|
| 167 |
-
gr.Textbox(label="Personal Access Token", type="password"),
|
| 168 |
-
gr.Textbox(label="Gemini API Key", type="password"),
|
| 169 |
-
gr.Radio(["User Guide", "Administration Guide"], label="Guide Type"),
|
| 170 |
-
gr.Textbox(label="Exclude Folders", placeholder="folder1, folder2, folder3")
|
| 171 |
-
],
|
| 172 |
-
outputs=[
|
| 173 |
-
gr.Textbox(label="Generated Guide"),
|
| 174 |
-
gr.File(label="Download Guide (DOCX)"),
|
| 175 |
-
gr.File(label="Download Guide (Markdown)")
|
| 176 |
-
],
|
| 177 |
-
title="Automated Guide Generator",
|
| 178 |
-
description="Generate a user guide or administration guide based on the UI-related code in a Git repository using Gemini AI. Select a Git provider, enter repository details, choose the guide type, and let AI create a comprehensive guide.",
|
| 179 |
-
allow_flagging="never",
|
| 180 |
-
theme="default",
|
| 181 |
-
analytics_enabled=False,
|
| 182 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
if __name__ ==
|
| 185 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import dash
|
| 2 |
+
from dash import dcc, html, Input, Output, State, callback
|
| 3 |
+
import dash_bootstrap_components as dbc
|
| 4 |
+
from dash.exceptions import PreventUpdate
|
| 5 |
import google.generativeai as genai
|
| 6 |
from github import Github
|
| 7 |
import gitlab
|
|
|
|
| 10 |
import docx
|
| 11 |
import os
|
| 12 |
import logging
|
| 13 |
+
import threading
|
| 14 |
+
import base64
|
| 15 |
|
| 16 |
# Configure logging
|
| 17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
+
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Helper functions (is_ui_file, get_file_contents, generate_guide_section, generate_guide)
|
| 23 |
+
# ... (Keep these functions as they are in the original code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
app.layout = dbc.Container([
|
| 26 |
+
html.H1("Automated Guide Generator", className="my-4"),
|
| 27 |
+
html.P("Generate a user guide or administration guide based on the UI-related code in a Git repository using Gemini AI. Select a Git provider, enter repository details, choose the guide type, and let AI create a comprehensive guide."),
|
|
|
|
| 28 |
|
| 29 |
+
dbc.Card([
|
| 30 |
+
dbc.CardBody([
|
| 31 |
+
dcc.Dropdown(
|
| 32 |
+
id='git-provider',
|
| 33 |
+
options=[
|
| 34 |
+
{'label': 'GitHub', 'value': 'GitHub'},
|
| 35 |
+
{'label': 'GitLab', 'value': 'GitLab'},
|
| 36 |
+
{'label': 'Gitea', 'value': 'Gitea'}
|
| 37 |
+
],
|
| 38 |
+
placeholder="Select Git Provider"
|
| 39 |
+
),
|
| 40 |
+
dbc.Input(id='repo-url', placeholder="Repository URL (owner/repo)", type="text", className="mt-3"),
|
| 41 |
+
dcc.RadioItems(
|
| 42 |
+
id='guide-type',
|
| 43 |
+
options=[
|
| 44 |
+
{'label': 'User Guide', 'value': 'User Guide'},
|
| 45 |
+
{'label': 'Administration Guide', 'value': 'Administration Guide'}
|
| 46 |
+
],
|
| 47 |
+
className="mt-3"
|
| 48 |
+
),
|
| 49 |
+
dbc.Input(id='exclude-folders', placeholder="Exclude Folders (comma-separated)", type="text", className="mt-3"),
|
| 50 |
+
dbc.Button("Generate Guide", id="generate-button", color="primary", className="mt-3"),
|
| 51 |
+
])
|
| 52 |
+
], className="mb-4"),
|
| 53 |
+
|
| 54 |
+
dbc.Spinner(
|
| 55 |
+
dcc.Loading(
|
| 56 |
+
id="loading-output",
|
| 57 |
+
children=[
|
| 58 |
+
html.Div(id="output-area"),
|
| 59 |
+
dcc.Download(id="download-docx"),
|
| 60 |
+
dcc.Download(id="download-md")
|
| 61 |
+
],
|
| 62 |
+
type="default",
|
| 63 |
+
)
|
| 64 |
+
),
|
| 65 |
+
|
| 66 |
+
dcc.Store(id='guide-store'),
|
| 67 |
+
])
|
| 68 |
|
| 69 |
+
@app.callback(
|
| 70 |
+
Output('guide-store', 'data'),
|
| 71 |
+
Output('output-area', 'children'),
|
| 72 |
+
Input('generate-button', 'n_clicks'),
|
| 73 |
+
State('git-provider', 'value'),
|
| 74 |
+
State('repo-url', 'value'),
|
| 75 |
+
State('guide-type', 'value'),
|
| 76 |
+
State('exclude-folders', 'value'),
|
| 77 |
+
prevent_initial_call=True
|
| 78 |
+
)
|
| 79 |
+
def generate_guide_callback(n_clicks, git_provider, repo_url, guide_type, exclude_folders):
|
| 80 |
+
if not all([git_provider, repo_url, guide_type]):
|
| 81 |
+
raise PreventUpdate
|
| 82 |
|
| 83 |
+
def generate_guide_thread():
|
| 84 |
+
try:
|
| 85 |
+
guide_text, docx_path, md_path = generate_guide(
|
| 86 |
+
git_provider, repo_url, "", "", guide_type, exclude_folders
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
with open(docx_path, 'rb') as docx_file, open(md_path, 'rb') as md_file:
|
| 90 |
+
docx_content = base64.b64encode(docx_file.read()).decode('utf-8')
|
| 91 |
+
md_content = base64.b64encode(md_file.read()).decode('utf-8')
|
| 92 |
+
|
| 93 |
+
os.unlink(docx_path)
|
| 94 |
+
os.unlink(md_path)
|
| 95 |
+
|
| 96 |
+
return {
|
| 97 |
+
'guide_text': guide_text,
|
| 98 |
+
'docx_content': docx_content,
|
| 99 |
+
'md_content': md_content
|
| 100 |
+
}
|
| 101 |
+
except Exception as e:
|
| 102 |
+
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
| 103 |
+
return {'error': str(e)}
|
| 104 |
|
| 105 |
+
thread = threading.Thread(target=generate_guide_thread)
|
| 106 |
+
thread.start()
|
| 107 |
+
thread.join()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
result = thread.result() if hasattr(thread, 'result') else None
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
if result and 'error' not in result:
|
| 112 |
+
output = [
|
| 113 |
+
html.H3("Generated Guide"),
|
| 114 |
+
html.Pre(result['guide_text']),
|
| 115 |
+
dbc.Button("Download DOCX", id="btn-download-docx", color="secondary", className="me-2"),
|
| 116 |
+
dbc.Button("Download Markdown", id="btn-download-md", color="secondary")
|
| 117 |
+
]
|
| 118 |
+
return result, output
|
| 119 |
+
else:
|
| 120 |
+
error_message = result['error'] if result and 'error' in result else "An unknown error occurred"
|
| 121 |
+
return None, html.Div(f"Error: {error_message}", style={'color': 'red'})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
@app.callback(
|
| 124 |
+
Output("download-docx", "data"),
|
| 125 |
+
Input("btn-download-docx", "n_clicks"),
|
| 126 |
+
State('guide-store', 'data'),
|
| 127 |
+
prevent_initial_call=True
|
| 128 |
+
)
|
| 129 |
+
def download_docx(n_clicks, data):
|
| 130 |
+
if data and 'docx_content' in data:
|
| 131 |
+
return dict(content=data['docx_content'], filename="guide.docx", base64=True)
|
| 132 |
+
raise PreventUpdate
|
|
|
|
|
|
|
| 133 |
|
| 134 |
+
@app.callback(
|
| 135 |
+
Output("download-md", "data"),
|
| 136 |
+
Input("btn-download-md", "n_clicks"),
|
| 137 |
+
State('guide-store', 'data'),
|
| 138 |
+
prevent_initial_call=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
)
|
| 140 |
+
def download_md(n_clicks, data):
|
| 141 |
+
if data and 'md_content' in data:
|
| 142 |
+
return dict(content=data['md_content'], filename="guide.md", base64=True)
|
| 143 |
+
raise PreventUpdate
|
| 144 |
|
| 145 |
+
if __name__ == '__main__':
|
| 146 |
+
print("Starting the Dash application...")
|
| 147 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|
| 148 |
+
print("Dash application has finished running.")
|