Update app.py
Browse files
app.py
CHANGED
|
@@ -5,34 +5,33 @@ from docx import Document
|
|
| 5 |
from docx.shared import Inches
|
| 6 |
from datetime import datetime
|
| 7 |
import streamlit as st
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
openai.api_key = openai_api_key # Set the OpenAI API key
|
| 12 |
|
| 13 |
# Check if the API key is loaded correctly
|
| 14 |
-
if not
|
| 15 |
st.error("OpenAI API key is missing in Streamlit secrets.")
|
| 16 |
else:
|
| 17 |
st.write("OpenAI API Key loaded successfully.")
|
| 18 |
|
| 19 |
def generate_text(prompt, role="user"):
|
| 20 |
-
"""Generate text using the OpenAI API."""
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
model="gpt-4", # Use the model you want
|
| 25 |
messages=[
|
| 26 |
{"role": "system", "content": "You are an expert banking analyst."},
|
| 27 |
{"role": role, "content": prompt}
|
| 28 |
]
|
| 29 |
)
|
| 30 |
-
return response
|
| 31 |
except Exception as e:
|
| 32 |
st.error(f"Error generating text: {e}")
|
| 33 |
return "(*Failed to generate content. Please review manually.*)"
|
| 34 |
|
| 35 |
-
#
|
| 36 |
doc = Document()
|
| 37 |
doc.add_heading('Functional Specification Document (FSD)', level=1)
|
| 38 |
doc.add_paragraph("**Project Name:** Banking Loan & Fraud Detection System")
|
|
@@ -106,11 +105,10 @@ ax.set_xticks([])
|
|
| 106 |
ax.set_yticks([])
|
| 107 |
ax.set_frame_on(False)
|
| 108 |
|
| 109 |
-
# Save diagram
|
| 110 |
output_folder = "output"
|
| 111 |
os.makedirs(output_folder, exist_ok=True)
|
| 112 |
|
| 113 |
-
# Check if the folder is created
|
| 114 |
if os.path.exists(output_folder):
|
| 115 |
st.write(f"Output folder exists: {output_folder}")
|
| 116 |
else:
|
|
@@ -120,7 +118,7 @@ diagram_path = os.path.join(output_folder, "banking_system_architecture.png")
|
|
| 120 |
plt.savefig(diagram_path)
|
| 121 |
plt.close(fig)
|
| 122 |
|
| 123 |
-
# Insert diagram into
|
| 124 |
doc.add_picture(diagram_path, width=Inches(5))
|
| 125 |
|
| 126 |
# Process Flow Diagrams
|
|
@@ -154,12 +152,11 @@ doc.add_paragraph("(*Placeholder for glossary of terms related to banking, loans
|
|
| 154 |
doc.add_heading("13. References", level=2)
|
| 155 |
doc.add_paragraph("(*Placeholder for references including documents, articles, and regulatory guidelines*)")
|
| 156 |
|
| 157 |
-
# Save
|
| 158 |
doc_path = os.path.join(output_folder, "Banking_FSD.docx")
|
| 159 |
doc.save(doc_path)
|
| 160 |
|
| 161 |
-
# Confirm file creation
|
| 162 |
if os.path.exists(doc_path):
|
| 163 |
-
st.write(f"
|
| 164 |
else:
|
| 165 |
st.error("Failed to generate the FSD document.")
|
|
|
|
| 5 |
from docx.shared import Inches
|
| 6 |
from datetime import datetime
|
| 7 |
import streamlit as st
|
| 8 |
+
from openai import OpenAI # New import style
|
| 9 |
|
| 10 |
+
# Initialize OpenAI client
|
| 11 |
+
client = OpenAI(api_key=st.secrets.get("OPENAI_API_KEY"))
|
|
|
|
| 12 |
|
| 13 |
# Check if the API key is loaded correctly
|
| 14 |
+
if not client.api_key:
|
| 15 |
st.error("OpenAI API key is missing in Streamlit secrets.")
|
| 16 |
else:
|
| 17 |
st.write("OpenAI API Key loaded successfully.")
|
| 18 |
|
| 19 |
def generate_text(prompt, role="user"):
|
| 20 |
+
"""Generate text using the OpenAI API (v1.0+ compatible)"""
|
| 21 |
try:
|
| 22 |
+
response = client.chat.completions.create(
|
| 23 |
+
model="gpt-4-1106-preview", # Updated model name
|
|
|
|
| 24 |
messages=[
|
| 25 |
{"role": "system", "content": "You are an expert banking analyst."},
|
| 26 |
{"role": role, "content": prompt}
|
| 27 |
]
|
| 28 |
)
|
| 29 |
+
return response.choices[0].message.content
|
| 30 |
except Exception as e:
|
| 31 |
st.error(f"Error generating text: {e}")
|
| 32 |
return "(*Failed to generate content. Please review manually.*)"
|
| 33 |
|
| 34 |
+
# Document creation remains the same
|
| 35 |
doc = Document()
|
| 36 |
doc.add_heading('Functional Specification Document (FSD)', level=1)
|
| 37 |
doc.add_paragraph("**Project Name:** Banking Loan & Fraud Detection System")
|
|
|
|
| 105 |
ax.set_yticks([])
|
| 106 |
ax.set_frame_on(False)
|
| 107 |
|
| 108 |
+
# Save diagram
|
| 109 |
output_folder = "output"
|
| 110 |
os.makedirs(output_folder, exist_ok=True)
|
| 111 |
|
|
|
|
| 112 |
if os.path.exists(output_folder):
|
| 113 |
st.write(f"Output folder exists: {output_folder}")
|
| 114 |
else:
|
|
|
|
| 118 |
plt.savefig(diagram_path)
|
| 119 |
plt.close(fig)
|
| 120 |
|
| 121 |
+
# Insert diagram into document
|
| 122 |
doc.add_picture(diagram_path, width=Inches(5))
|
| 123 |
|
| 124 |
# Process Flow Diagrams
|
|
|
|
| 152 |
doc.add_heading("13. References", level=2)
|
| 153 |
doc.add_paragraph("(*Placeholder for references including documents, articles, and regulatory guidelines*)")
|
| 154 |
|
| 155 |
+
# Save Document
|
| 156 |
doc_path = os.path.join(output_folder, "Banking_FSD.docx")
|
| 157 |
doc.save(doc_path)
|
| 158 |
|
|
|
|
| 159 |
if os.path.exists(doc_path):
|
| 160 |
+
st.write(f"FSD generated successfully at: {doc_path}")
|
| 161 |
else:
|
| 162 |
st.error("Failed to generate the FSD document.")
|