bluenevus commited on
Commit
0a1b210
·
verified ·
1 Parent(s): e60c00e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -45
app.py CHANGED
@@ -3,70 +3,80 @@ import io
3
  import os
4
  import threading
5
  import time
6
- from typing import List, Dict
7
 
8
  import dash
9
  import dash_bootstrap_components as dbc
10
- from dash import html, dcc, callback, Input, Output, State
11
  import google.generativeai as genai
12
  from docx import Document
 
13
 
14
  # Initialize Dash app
15
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
16
 
17
- # Gemini API setup
18
- genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
19
  model = genai.GenerativeModel('gemini-pro')
20
 
21
  def process_document(contents: str, filename: str) -> str:
22
  content_type, content_string = contents.split(',')
23
  decoded = base64.b64decode(content_string)
24
 
25
- if filename.endswith('.docx'):
 
 
 
 
 
26
  doc = Document(io.BytesIO(decoded))
27
- text = '\n'.join([para.text for para in doc.paragraphs])
28
- else: # Assume it's a .doc file
29
- text = decoded.decode('utf-8')
30
 
31
  return text
32
 
33
  def generate_outline(text: str) -> str:
34
  prompt = f"""
35
- Analyze the following Performance Work Statement (PWS), focusing on sections L&M:
 
 
 
36
  {text}
37
-
38
- Generate an outline that follows the PWS headers and sub-headers, summarizing specific requirements in each section.
39
- Format the outline with clear hierarchy and brief summaries.
40
  """
41
  response = model.generate_content(prompt)
42
  return response.text
43
 
44
- def generate_pink_team_response(outline: str) -> str:
45
  prompt = f"""
46
- Based on the following outline of a Performance Work Statement (PWS):
 
47
  {outline}
48
-
49
- Draft a detailed response as if MicroHealth is responding to the PWS. Follow these guidelines:
50
- 1. Write in Wikipedia style with active voice.
51
- 2. Describe in detail how MicroHealth will innovate to address each requirement.
52
- 3. Explain the industry best practices applied.
 
53
  4. Provide measurable outcomes for the customer.
54
  5. Limit the use of bullet points and write predominantly in paragraph format.
55
  6. Ensure a logical flow of steps taken by MicroHealth for each requirement.
56
-
57
- Your response should be comprehensive and professional, showcasing MicroHealth's expertise and approach.
58
  """
59
  response = model.generate_content(prompt)
60
  return response.text
61
 
62
  # Layout
63
  app.layout = dbc.Container([
64
- html.H1("MicroHealth PWS Analysis Tool", className="my-4"),
65
  dbc.Tabs([
66
  dbc.Tab(label="Shred", tab_id="shred", children=[
67
  dcc.Upload(
68
  id='upload-document',
69
- children=html.Div(['Drag and Drop or ', html.A('Select a File')]),
70
  style={
71
  'width': '100%',
72
  'height': '60px',
@@ -79,44 +89,65 @@ app.layout = dbc.Container([
79
  },
80
  multiple=False
81
  ),
82
- html.Div(id='output-document-upload'),
83
- dbc.Spinner(html.Div(id='output-outline')),
 
84
  ]),
85
- dbc.Tab(label="Pink", tab_id="pink-team", children=[
86
- dbc.Button("Generate Pink Team Response", id="generate-pink-team", color="primary", className="mt-3"),
87
- dbc.Spinner(html.Div(id='output-pink-team')),
 
 
88
  ]),
89
  ], id="tabs", active_tab="shred"),
90
  ])
91
 
92
  @app.callback(
93
- Output('output-document-upload', 'children'),
94
- Output('output-outline', 'children'),
95
  Input('upload-document', 'contents'),
96
- State('upload-document', 'filename'),
97
- prevent_initial_call=True
98
  )
99
- def update_output(contents, filename):
100
  if contents is None:
101
- return "No file uploaded.", ""
102
 
103
  text = process_document(contents, filename)
104
  outline = generate_outline(text)
105
-
106
- return f"Processed file: {filename}", dcc.Markdown(outline)
107
 
108
  @app.callback(
109
- Output('output-pink-team', 'children'),
110
- Input('generate-pink-team', 'n_clicks'),
111
- State('output-outline', 'children'),
112
- prevent_initial_call=True
113
  )
114
- def update_pink_team(n_clicks, outline):
115
- if outline is None:
116
- return "Please generate an outline first."
117
 
118
- pink_team_response = generate_pink_team_response(outline['props']['children'])
119
- return dcc.Markdown(pink_team_response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  if __name__ == '__main__':
122
  print("Starting the Dash application...")
 
3
  import os
4
  import threading
5
  import time
6
+ from typing import List, Tuple
7
 
8
  import dash
9
  import dash_bootstrap_components as dbc
10
+ from dash import html, dcc, Input, Output, State, ctx
11
  import google.generativeai as genai
12
  from docx import Document
13
+ from PyPDF2 import PdfReader
14
 
15
  # Initialize Dash app
16
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
17
 
18
+ # Configure Gemini AI
19
+ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
20
  model = genai.GenerativeModel('gemini-pro')
21
 
22
  def process_document(contents: str, filename: str) -> str:
23
  content_type, content_string = contents.split(',')
24
  decoded = base64.b64decode(content_string)
25
 
26
+ if filename.endswith('.pdf'):
27
+ pdf = PdfReader(io.BytesIO(decoded))
28
+ text = ""
29
+ for page in pdf.pages:
30
+ text += page.extract_text()
31
+ elif filename.endswith('.docx'):
32
  doc = Document(io.BytesIO(decoded))
33
+ text = "\n".join([para.text for para in doc.paragraphs])
34
+ else:
35
+ return "Unsupported file format. Please upload a PDF or DOCX file."
36
 
37
  return text
38
 
39
  def generate_outline(text: str) -> str:
40
  prompt = f"""
41
+ Analyze the following Project Work Statement (PWS) and create an outline
42
+ focusing on sections L&M. Extract the main headers, subheaders, and specific
43
+ requirements in each section. Summarize the key points:
44
+
45
  {text}
46
+
47
+ Provide the outline in a structured format.
 
48
  """
49
  response = model.generate_content(prompt)
50
  return response.text
51
 
52
+ def generate_pink_team_document(outline: str) -> str:
53
  prompt = f"""
54
+ Based on the following outline of a Project Work Statement (PWS):
55
+
56
  {outline}
57
+
58
+ Create a detailed response document as if MicroHealth is responding to this PWS.
59
+ Follow these guidelines:
60
+ 1. Use Wikipedia style writing with active voice.
61
+ 2. For each requirement, describe in detail how MicroHealth will innovate to address it.
62
+ 3. Explain the industry best practices that will be applied.
63
  4. Provide measurable outcomes for the customer.
64
  5. Limit the use of bullet points and write predominantly in paragraph format.
65
  6. Ensure a logical flow of steps taken by MicroHealth for each requirement.
66
+
67
+ Generate a comprehensive response that showcases MicroHealth's expertise and approach.
68
  """
69
  response = model.generate_content(prompt)
70
  return response.text
71
 
72
  # Layout
73
  app.layout = dbc.Container([
74
+ html.H1("MicroHealth PWS Analysis and Response Generator", className="my-4"),
75
  dbc.Tabs([
76
  dbc.Tab(label="Shred", tab_id="shred", children=[
77
  dcc.Upload(
78
  id='upload-document',
79
+ children=html.Div(['Drag and Drop or ', html.A('Select Files')]),
80
  style={
81
  'width': '100%',
82
  'height': '60px',
 
89
  },
90
  multiple=False
91
  ),
92
+ dbc.Spinner(html.Div(id='shred-output')),
93
+ dbc.Button("Download Outline", id="download-shred", className="mt-3"),
94
+ dcc.Download(id="download-shred-doc")
95
  ]),
96
+ dbc.Tab(label="Pink", tab_id="pink", children=[
97
+ dbc.Button("Generate Pink Team Document", id="generate-pink", className="mt-3"),
98
+ dbc.Spinner(html.Div(id='pink-output')),
99
+ dbc.Button("Download Pink Team Document", id="download-pink", className="mt-3"),
100
+ dcc.Download(id="download-pink-doc")
101
  ]),
102
  ], id="tabs", active_tab="shred"),
103
  ])
104
 
105
  @app.callback(
106
+ Output('shred-output', 'children'),
 
107
  Input('upload-document', 'contents'),
108
+ State('upload-document', 'filename')
 
109
  )
110
+ def update_shred_output(contents, filename):
111
  if contents is None:
112
+ return "Upload a document to begin."
113
 
114
  text = process_document(contents, filename)
115
  outline = generate_outline(text)
116
+ return dcc.Markdown(outline)
 
117
 
118
  @app.callback(
119
+ Output('pink-output', 'children'),
120
+ Input('generate-pink', 'n_clicks'),
121
+ State('shred-output', 'children')
 
122
  )
123
+ def update_pink_output(n_clicks, shred_output):
124
+ if n_clicks is None or shred_output is None:
125
+ return "Generate an outline in the Shred tab first."
126
 
127
+ pink_doc = generate_pink_team_document(shred_output)
128
+ return dcc.Markdown(pink_doc)
129
+
130
+ @app.callback(
131
+ Output("download-shred-doc", "data"),
132
+ Input("download-shred", "n_clicks"),
133
+ State('shred-output', 'children'),
134
+ prevent_initial_call=True,
135
+ )
136
+ def download_shred(n_clicks, shred_output):
137
+ if shred_output is None:
138
+ return dash.no_update
139
+ return dict(content=shred_output, filename="shred_outline.md")
140
+
141
+ @app.callback(
142
+ Output("download-pink-doc", "data"),
143
+ Input("download-pink", "n_clicks"),
144
+ State('pink-output', 'children'),
145
+ prevent_initial_call=True,
146
+ )
147
+ def download_pink(n_clicks, pink_output):
148
+ if pink_output is None:
149
+ return dash.no_update
150
+ return dict(content=pink_output, filename="pink_team_document.md")
151
 
152
  if __name__ == '__main__':
153
  print("Starting the Dash application...")