bluenevus commited on
Commit
7198283
·
verified ·
1 Parent(s): 2f1ac05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -34
app.py CHANGED
@@ -10,9 +10,11 @@ from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_excep
10
  import threading
11
  import os
12
  from io import BytesIO
 
 
13
 
14
  # Hugging Face variables
15
- GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN')
16
  GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY')
17
 
18
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
@@ -20,7 +22,7 @@ app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
20
  # Global variable to store generated file
21
  generated_file = None
22
 
23
- def fetch_git_files(git_url, personal_access_token, git_provider):
24
  try:
25
  # Parse the Git URL
26
  parts = git_url.split('/')
@@ -38,7 +40,7 @@ def fetch_git_files(git_url, personal_access_token, git_provider):
38
 
39
  # Set up headers with the personal access token
40
  headers = {
41
- "Authorization": f"token {personal_access_token}",
42
  "Accept": "application/vnd.github.v3+json"
43
  }
44
 
@@ -120,34 +122,34 @@ def process_with_gemini(file_content, gemini_api_key):
120
  combined_result = "\n\n".join(results)
121
  return combined_result
122
 
123
- def process_input(git_url, personal_access_token, git_provider):
124
  global generated_file
125
  generated_file = None
126
 
127
  if not git_url.startswith(f"https://{git_provider.lower()}.com/"):
128
- return f"Error: Invalid {git_provider} URL. Please use the format: https://{git_provider.lower()}.com/username/repository.git"
129
- if not personal_access_token.strip():
130
- return "Error: Personal Access Token is empty. Please provide a valid token."
131
 
132
- file_content = fetch_git_files(git_url, personal_access_token, git_provider)
133
  if file_content.startswith("Error:"):
134
- return file_content
135
 
136
  try:
137
  # Process the file content with Gemini
138
  analysis = process_with_gemini(file_content, GEMINI_API_KEY)
139
  generated_file = analysis.encode()
140
- return "Analysis complete. Click the download button to get the results."
141
  except Exception as e:
142
- return f"Error processing the files: {str(e)}"
143
 
144
  app.layout = dbc.Container([
145
- html.H1("Open Source License Extractor", className="my-4"),
146
- html.P("Provide a Git repository URL to analyze open-source licenses from dependency files.", className="mb-4"),
147
- dbc.Card([
148
- dbc.CardBody([
149
- dbc.Row([
150
- dbc.Col([
 
 
151
  dcc.Dropdown(
152
  id='git-provider',
153
  options=[
@@ -159,11 +161,7 @@ app.layout = dbc.Container([
159
  className="mb-3"
160
  ),
161
  dbc.Input(id="git-url", placeholder="Enter Git Repository URL", type="text", className="mb-3"),
162
- dbc.Input(id="personal-access-token", placeholder="Enter Git Personal Access Token", type="password", className="mb-3"),
163
  dbc.Button("Analyze", id="analyze-button", color="primary", className="mb-3"),
164
- dbc.Button("Download Results", id="download-button", color="secondary", className="mb-3 ml-2", disabled=True),
165
- dcc.Download(id="download-analysis"),
166
- html.Div(id="output", className="mt-3"),
167
  dcc.Loading(
168
  id="loading",
169
  type="dot",
@@ -171,44 +169,71 @@ app.layout = dbc.Container([
171
  )
172
  ])
173
  ])
174
- ])
 
 
 
 
 
 
 
 
 
 
 
 
175
  ])
176
  ], fluid=True)
177
 
178
  @app.callback(
179
  [Output("output", "children"),
180
- Output("download-button", "disabled"),
 
181
  Output("loading-output", "children")],
182
  [Input("analyze-button", "n_clicks")],
183
  [State("git-url", "value"),
184
- State("personal-access-token", "value"),
185
  State("git-provider", "value")],
186
  prevent_initial_call=True
187
  )
188
- def update_output(n_clicks, git_url, personal_access_token, git_provider):
189
  if n_clicks is None:
190
  raise PreventUpdate
191
 
192
  def process():
193
  global generated_file
194
- result = process_input(git_url, personal_access_token, git_provider)
195
- return result, generated_file is not None, ""
196
 
197
  return process()
198
 
199
  @app.callback(
200
- Output("download-analysis", "data"),
201
- Input("download-button", "n_clicks"),
202
  prevent_initial_call=True
203
  )
204
- def download_analysis(n_clicks):
205
- if n_clicks is None:
206
  raise PreventUpdate
 
 
 
 
 
 
 
207
 
208
- if generated_file is None:
209
- return dash.no_update
 
 
 
 
 
 
 
 
210
 
211
- return dcc.send_bytes(generated_file, "license_analysis.txt")
212
 
213
  if __name__ == '__main__':
214
  print("Starting the Dash application...")
 
10
  import threading
11
  import os
12
  from io import BytesIO
13
+ from docx import Document
14
+ import markdown
15
 
16
  # Hugging Face variables
17
+ GIT_TOKEN = os.environ.get('GIT_TOKEN')
18
  GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY')
19
 
20
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
 
22
  # Global variable to store generated file
23
  generated_file = None
24
 
25
+ def fetch_git_files(git_url, git_provider):
26
  try:
27
  # Parse the Git URL
28
  parts = git_url.split('/')
 
40
 
41
  # Set up headers with the personal access token
42
  headers = {
43
+ "Authorization": f"token {GIT_TOKEN}",
44
  "Accept": "application/vnd.github.v3+json"
45
  }
46
 
 
122
  combined_result = "\n\n".join(results)
123
  return combined_result
124
 
125
+ def process_input(git_url, git_provider):
126
  global generated_file
127
  generated_file = None
128
 
129
  if not git_url.startswith(f"https://{git_provider.lower()}.com/"):
130
+ return f"Error: Invalid {git_provider} URL. Please use the format: https://{git_provider.lower()}.com/username/repository.git", None
 
 
131
 
132
+ file_content = fetch_git_files(git_url, git_provider)
133
  if file_content.startswith("Error:"):
134
+ return file_content, None
135
 
136
  try:
137
  # Process the file content with Gemini
138
  analysis = process_with_gemini(file_content, GEMINI_API_KEY)
139
  generated_file = analysis.encode()
140
+ return "Analysis complete. You can now preview the results and download them.", analysis
141
  except Exception as e:
142
+ return f"Error processing the files: {str(e)}", None
143
 
144
  app.layout = dbc.Container([
145
+ dbc.Row([
146
+ dbc.Col(html.H1("Open Source License Extractor", className="my-4"), width=12)
147
+ ]),
148
+ dbc.Row([
149
+ dbc.Col([
150
+ dbc.Card([
151
+ dbc.CardBody([
152
+ html.P("Provide a Git repository URL to analyze open-source licenses from dependency files.", className="mb-4"),
153
  dcc.Dropdown(
154
  id='git-provider',
155
  options=[
 
161
  className="mb-3"
162
  ),
163
  dbc.Input(id="git-url", placeholder="Enter Git Repository URL", type="text", className="mb-3"),
 
164
  dbc.Button("Analyze", id="analyze-button", color="primary", className="mb-3"),
 
 
 
165
  dcc.Loading(
166
  id="loading",
167
  type="dot",
 
169
  )
170
  ])
171
  ])
172
+ ], md=6),
173
+ dbc.Col([
174
+ dbc.Card([
175
+ dbc.CardBody([
176
+ html.H4("Analysis Results", className="mb-3"),
177
+ html.Div(id="output", className="mb-3"),
178
+ dbc.Button("Download as Word", id="download-word-button", color="secondary", className="mb-3 me-2", disabled=True),
179
+ dbc.Button("Download as Markdown", id="download-markdown-button", color="secondary", className="mb-3", disabled=True),
180
+ dcc.Download(id="download-word"),
181
+ dcc.Download(id="download-markdown"),
182
+ ])
183
+ ])
184
+ ], md=6)
185
  ])
186
  ], fluid=True)
187
 
188
  @app.callback(
189
  [Output("output", "children"),
190
+ Output("download-word-button", "disabled"),
191
+ Output("download-markdown-button", "disabled"),
192
  Output("loading-output", "children")],
193
  [Input("analyze-button", "n_clicks")],
194
  [State("git-url", "value"),
 
195
  State("git-provider", "value")],
196
  prevent_initial_call=True
197
  )
198
+ def update_output(n_clicks, git_url, git_provider):
199
  if n_clicks is None:
200
  raise PreventUpdate
201
 
202
  def process():
203
  global generated_file
204
+ result, analysis = process_input(git_url, git_provider)
205
+ return analysis if analysis else result, generated_file is None, generated_file is None, ""
206
 
207
  return process()
208
 
209
  @app.callback(
210
+ Output("download-word", "data"),
211
+ Input("download-word-button", "n_clicks"),
212
  prevent_initial_call=True
213
  )
214
+ def download_word(n_clicks):
215
+ if n_clicks is None or generated_file is None:
216
  raise PreventUpdate
217
+
218
+ doc = Document()
219
+ doc.add_paragraph(generated_file.decode())
220
+
221
+ buffer = BytesIO()
222
+ doc.save(buffer)
223
+ buffer.seek(0)
224
 
225
+ return dcc.send_bytes(buffer.getvalue(), "license_analysis.docx")
226
+
227
+ @app.callback(
228
+ Output("download-markdown", "data"),
229
+ Input("download-markdown-button", "n_clicks"),
230
+ prevent_initial_call=True
231
+ )
232
+ def download_markdown(n_clicks):
233
+ if n_clicks is None or generated_file is None:
234
+ raise PreventUpdate
235
 
236
+ return dcc.send_string(generated_file.decode(), "license_analysis.md")
237
 
238
  if __name__ == '__main__':
239
  print("Starting the Dash application...")