RAHULJUNEJA33 commited on
Commit
c4df43e
·
verified ·
1 Parent(s): 7edb496

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
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
- # Retrieve the OpenAI API Key from Streamlit secrets
10
- openai_api_key = st.secrets["OPENAI_API_KEY"]
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 openai_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."""
21
  try:
22
- # Make a request to OpenAI's new API using `openai.Completion.create()`
23
- response = openai.Completion.create(
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['choices'][0]['message']['content']
31
  except Exception as e:
32
  st.error(f"Error generating text: {e}")
33
  return "(*Failed to generate content. Please review manually.*)"
34
 
35
- # Create the Word document
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 in a designated output folder
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 the document
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 the Word Document
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"Functional Specification Document (FSD) generated successfully at: {doc_path}")
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.")