Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,42 @@
|
|
|
|
|
| 1 |
import openai
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
from docx import Document
|
| 4 |
from docx.shared import Inches
|
| 5 |
-
from huggingface_hub import
|
| 6 |
|
| 7 |
-
# β
Retrieve API key securely from Hugging Face secret store
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# β
Configure OpenAI API Key
|
| 14 |
-
openai.api_key = OPENAI_API_KEY
|
| 15 |
|
| 16 |
def generate_text(prompt):
|
| 17 |
"""Generate text using OpenAI GPT securely"""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# β
Create Word Document
|
| 28 |
doc = Document()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import openai
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
from docx import Document
|
| 5 |
from docx.shared import Inches
|
| 6 |
+
from huggingface_hub import HfApi
|
| 7 |
|
| 8 |
+
# β
Retrieve API key securely from Hugging Face secret store (using environment variables)
|
| 9 |
+
def retrieve_openai_api_key():
|
| 10 |
+
"""Retrieve OpenAI API Key securely from Hugging Face Secrets storage."""
|
| 11 |
+
# Fetch the API Key using the Hugging Face API
|
| 12 |
+
api = HfApi()
|
| 13 |
+
try:
|
| 14 |
+
openai_api_key = api.get_secret("OPENAI_API_KEY") # Fetch API key from Hugging Face Secrets
|
| 15 |
+
if openai_api_key is None:
|
| 16 |
+
raise ValueError("OPENAI API Key not found in Hugging Face Secrets.")
|
| 17 |
+
return openai_api_key
|
| 18 |
+
except Exception as e:
|
| 19 |
+
raise RuntimeError(f"Error retrieving OpenAI API Key: {str(e)}") from e
|
| 20 |
+
|
| 21 |
+
# β
Retrieve API key from Hugging Face secret store
|
| 22 |
+
OPENAI_API_KEY = retrieve_openai_api_key()
|
| 23 |
|
| 24 |
# β
Configure OpenAI API Key
|
| 25 |
+
openai.api_key = OPENAI_API_KEY
|
| 26 |
|
| 27 |
def generate_text(prompt):
|
| 28 |
"""Generate text using OpenAI GPT securely"""
|
| 29 |
+
try:
|
| 30 |
+
response = openai.ChatCompletion.create(
|
| 31 |
+
model="gpt-4",
|
| 32 |
+
messages=[
|
| 33 |
+
{"role": "system", "content": "You are a banking expert creating an FSD."},
|
| 34 |
+
{"role": "user", "content": prompt}
|
| 35 |
+
]
|
| 36 |
+
)
|
| 37 |
+
return response["choices"][0]["message"]["content"]
|
| 38 |
+
except Exception as e:
|
| 39 |
+
raise RuntimeError(f"Error generating content with OpenAI API: {str(e)}") from e
|
| 40 |
|
| 41 |
# β
Create Word Document
|
| 42 |
doc = Document()
|