Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
from github import Github
|
4 |
+
import gitlab
|
5 |
+
import requests
|
6 |
+
import tempfile
|
7 |
+
import docx
|
8 |
+
|
9 |
+
def generate_guide(git_provider, repo_url, personal_access_token, gemini_api_key, guide_type):
|
10 |
+
try:
|
11 |
+
if git_provider == "GitHub":
|
12 |
+
g = Github(personal_access_token)
|
13 |
+
repo = g.get_repo(repo_url)
|
14 |
+
content = repo.get_contents("")
|
15 |
+
file_contents = ""
|
16 |
+
for file in content:
|
17 |
+
if file.name.endswith('.py'):
|
18 |
+
file_contents += f"\n\n{file.name}:\n{file.decoded_content.decode()}"
|
19 |
+
elif git_provider == "GitLab":
|
20 |
+
gl = gitlab.Gitlab(url='https://gitlab.com', private_token=personal_access_token)
|
21 |
+
project = gl.projects.get(repo_url)
|
22 |
+
items = project.repository_tree(recursive=True)
|
23 |
+
file_contents = ""
|
24 |
+
for item in items:
|
25 |
+
if item['type'] == 'blob' and item['name'].endswith('.py'):
|
26 |
+
file_content = project.files.get(item['path'], ref='main')
|
27 |
+
file_contents += f"\n\n{item['name']}:\n{file_content.decode().decode()}"
|
28 |
+
elif git_provider == "Gitea":
|
29 |
+
base_url = "https://gitea.com/api/v1"
|
30 |
+
headers = {"Authorization": f"token {personal_access_token}"}
|
31 |
+
response = requests.get(f"{base_url}/repos/{repo_url}/contents", headers=headers)
|
32 |
+
response.raise_for_status()
|
33 |
+
file_contents = ""
|
34 |
+
for item in response.json():
|
35 |
+
if item['type'] == 'file' and item['name'].endswith('.py'):
|
36 |
+
file_content = requests.get(item['download_url']).text
|
37 |
+
file_contents += f"\n\n{item['name']}:\n{file_content}"
|
38 |
+
else:
|
39 |
+
return "Unsupported Git provider", None, None
|
40 |
+
|
41 |
+
genai.configure(api_key=gemini_api_key)
|
42 |
+
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
43 |
+
|
44 |
+
if guide_type == "User Guide":
|
45 |
+
prompt = f"""Based on the following Python code, generate a comprehensive user guide:
|
46 |
+
|
47 |
+
{file_contents}
|
48 |
+
|
49 |
+
Please organize the user guide into sections such as:
|
50 |
+
1. Introduction
|
51 |
+
2. Getting Started
|
52 |
+
3. Features and Functionality
|
53 |
+
4. How to Use
|
54 |
+
5. Tips and Best Practices
|
55 |
+
6. Troubleshooting
|
56 |
+
7. FAQ
|
57 |
+
|
58 |
+
For each section, provide step-by-step instructions on how to use the software's features.
|
59 |
+
Explain each functionality that the end user will interact with.
|
60 |
+
Include examples where appropriate.
|
61 |
+
|
62 |
+
Important formatting instructions:
|
63 |
+
- The output should be in plain text
|
64 |
+
- Use clear section titles
|
65 |
+
- Number each step within sections
|
66 |
+
- Provide clear, concise instructions for each step
|
67 |
+
- Explain the purpose and benefit of each feature for non-technical users
|
68 |
+
- Do not include any installation or configuration instructions
|
69 |
+
"""
|
70 |
+
else: # Administration Guide
|
71 |
+
prompt = f"""Based on the following Python code, generate a comprehensive administration guide:
|
72 |
+
|
73 |
+
{file_contents}
|
74 |
+
|
75 |
+
Please organize the administration guide into sections such as:
|
76 |
+
1. System Overview
|
77 |
+
2. Security and Access Control
|
78 |
+
3. Performance Monitoring and Optimization
|
79 |
+
4. Backup and Recovery
|
80 |
+
5. Troubleshooting and Maintenance
|
81 |
+
6. Customization and Configuration
|
82 |
+
7. Best Practices for Administrators
|
83 |
+
|
84 |
+
For each section, provide detailed information and step-by-step instructions for system administrators.
|
85 |
+
Focus on backend operations, system management, and advanced configurations.
|
86 |
+
Include examples and potential scenarios an administrator might encounter.
|
87 |
+
|
88 |
+
Important formatting instructions:
|
89 |
+
- The output should be in plain text
|
90 |
+
- Use clear section titles
|
91 |
+
- Number each step within sections
|
92 |
+
- Provide clear, concise instructions for each step
|
93 |
+
- Explain the purpose and implications of each administrative task
|
94 |
+
- Include any relevant security considerations
|
95 |
+
"""
|
96 |
+
|
97 |
+
response = model.generate_content(prompt)
|
98 |
+
guide = response.text
|
99 |
+
|
100 |
+
# Create DOCX file
|
101 |
+
doc = docx.Document()
|
102 |
+
doc.add_heading(guide_type, 0)
|
103 |
+
|
104 |
+
for line in guide.split('\n'):
|
105 |
+
line = line.strip()
|
106 |
+
if line.endswith(':'):
|
107 |
+
doc.add_heading(line, level=1)
|
108 |
+
elif line.startswith('Step'):
|
109 |
+
doc.add_paragraph(line, style='List Number')
|
110 |
+
else:
|
111 |
+
doc.add_paragraph(line)
|
112 |
+
|
113 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as temp_docx:
|
114 |
+
doc.save(temp_docx.name)
|
115 |
+
docx_path = temp_docx.name
|
116 |
+
|
117 |
+
# Create Markdown file
|
118 |
+
markdown_content = f"# {guide_type}\n\n"
|
119 |
+
for line in guide.split('\n'):
|
120 |
+
line = line.strip()
|
121 |
+
if line.endswith(':'):
|
122 |
+
markdown_content += f"\n## {line}\n\n"
|
123 |
+
elif line.startswith('Step'):
|
124 |
+
markdown_content += f"1. {line[5:]}\n"
|
125 |
+
else:
|
126 |
+
markdown_content += f"{line}\n"
|
127 |
+
|
128 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.md', mode='w', encoding='utf-8') as temp_md:
|
129 |
+
temp_md.write(markdown_content)
|
130 |
+
md_path = temp_md.name
|
131 |
+
|
132 |
+
return guide, docx_path, md_path
|
133 |
+
|
134 |
+
except Exception as e:
|
135 |
+
return f"An error occurred: {str(e)}", None, None
|
136 |
+
|
137 |
+
iface = gr.Interface(
|
138 |
+
fn=generate_guide,
|
139 |
+
inputs=[
|
140 |
+
gr.Dropdown(
|
141 |
+
choices=["GitHub", "GitLab", "Gitea"],
|
142 |
+
label="Git Provider"
|
143 |
+
),
|
144 |
+
gr.Textbox(label="Repository URL", placeholder="owner/repo"),
|
145 |
+
gr.Textbox(label="Personal Access Token", type="password"),
|
146 |
+
gr.Textbox(label="Gemini API Key", type="password"),
|
147 |
+
gr.Radio(["User Guide", "Administration Guide"], label="Guide Type")
|
148 |
+
],
|
149 |
+
outputs=[
|
150 |
+
gr.Textbox(label="Generated Guide"),
|
151 |
+
gr.File(label="Download Guide (DOCX)"),
|
152 |
+
gr.File(label="Download Guide (Markdown)")
|
153 |
+
],
|
154 |
+
title="Automated Guide Generator",
|
155 |
+
description="Generate a user guide or administration guide based on the Python 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.",
|
156 |
+
allow_flagging="never",
|
157 |
+
theme="default",
|
158 |
+
analytics_enabled=False,
|
159 |
+
)
|
160 |
+
|
161 |
+
iface.launch()
|