RAHULJUNEJA33 commited on
Commit
59d9105
Β·
verified Β·
1 Parent(s): 3c8efae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -15
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 get_secret_value # Import secret retrieval function
6
 
7
- # βœ… Retrieve API key securely from Hugging Face secret store
8
- try:
9
- OPENAI_API_KEY = get_secret_value("OPENAI_API_KEY") # Fetch stored API key
10
- except Exception as e:
11
- raise RuntimeError("Failed to retrieve OpenAI API Key from Hugging Face Secrets.") from e
 
 
 
 
 
 
 
 
 
 
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
- response = openai.ChatCompletion.create(
19
- model="gpt-4",
20
- messages=[
21
- {"role": "system", "content": "You are a banking expert creating an FSD."},
22
- {"role": "user", "content": prompt}
23
- ]
24
- )
25
- return response["choices"][0]["message"]["content"]
 
 
 
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()