Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -3,70 +3,80 @@ import io
|
|
3 |
import os
|
4 |
import threading
|
5 |
import time
|
6 |
-
from typing import List,
|
7 |
|
8 |
import dash
|
9 |
import dash_bootstrap_components as dbc
|
10 |
-
from dash import html, dcc,
|
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
|
18 |
-
genai.configure(api_key=os.environ
|
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('.
|
|
|
|
|
|
|
|
|
|
|
26 |
doc = Document(io.BytesIO(decoded))
|
27 |
-
text =
|
28 |
-
else:
|
29 |
-
|
30 |
|
31 |
return text
|
32 |
|
33 |
def generate_outline(text: str) -> str:
|
34 |
prompt = f"""
|
35 |
-
Analyze the following
|
|
|
|
|
|
|
36 |
{text}
|
37 |
-
|
38 |
-
|
39 |
-
Format the outline with clear hierarchy and brief summaries.
|
40 |
"""
|
41 |
response = model.generate_content(prompt)
|
42 |
return response.text
|
43 |
|
44 |
-
def
|
45 |
prompt = f"""
|
46 |
-
Based on the following outline of a
|
|
|
47 |
{outline}
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
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 |
-
|
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
|
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
|
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
|
83 |
-
dbc.
|
|
|
84 |
]),
|
85 |
-
dbc.Tab(label="Pink", tab_id="pink
|
86 |
-
dbc.Button("Generate Pink Team
|
87 |
-
dbc.Spinner(html.Div(id='
|
|
|
|
|
88 |
]),
|
89 |
], id="tabs", active_tab="shred"),
|
90 |
])
|
91 |
|
92 |
@app.callback(
|
93 |
-
Output('output
|
94 |
-
Output('output-outline', 'children'),
|
95 |
Input('upload-document', 'contents'),
|
96 |
-
State('upload-document', 'filename')
|
97 |
-
prevent_initial_call=True
|
98 |
)
|
99 |
-
def
|
100 |
if contents is None:
|
101 |
-
return "
|
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('
|
110 |
-
Input('generate-pink
|
111 |
-
State('output
|
112 |
-
prevent_initial_call=True
|
113 |
)
|
114 |
-
def
|
115 |
-
if
|
116 |
-
return "
|
117 |
|
118 |
-
|
119 |
-
return dcc.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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...")
|