taimoor61 commited on
Commit
f0ef76e
·
1 Parent(s): bbacfe7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -14
app.py CHANGED
@@ -1,21 +1,15 @@
1
  import os
2
  import time
3
- from dotenv import load_dotenv
4
 
5
  import streamlit as st
6
  from langchain.llms import openai
7
 
8
- # Load environment variables (if using a .env file)
9
- # load_dotenv() # Uncomment this line if using a .env file
10
-
11
  # Function to get OpenAI response
12
  def get_openai_response(question):
13
  try:
14
- # Access API key securely (choose one of the methods below)
15
- # api_key = os.environ["OPENAI_API_KEY"] # Method 1: Using environment variables
16
- # api_key = "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" # Method 2: Direct inclusion (not recommended)
17
  llm = openai.OpenAI(
18
- model_name="text-davinci-003", temperature=0.5, openai_api_key=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
19
  )
20
  with st.spinner("Generating response..."):
21
  response = llm(question)
@@ -24,9 +18,6 @@ def get_openai_response(question):
24
  st.error(f"Error: {e}")
25
  return None
26
 
27
- # Rest of the code remains the same...
28
-
29
-
30
  # Initialize Streamlit app
31
  st.set_page_config(page_title="Enhanced Q&A Demo")
32
  st.header("Langchain App")
@@ -46,13 +37,12 @@ if submit_button:
46
  if response:
47
  st.subheader("The answer is:")
48
  st.write(response)
49
-
50
  # Add response to history
51
  response_history.code(f"""
52
- {input_text}
53
  **Answer:** {response}
54
  ---
55
  """)
56
  else:
57
  st.error("Something went wrong. Please try again later.")
58
-
 
1
  import os
2
  import time
 
3
 
4
  import streamlit as st
5
  from langchain.llms import openai
6
 
 
 
 
7
  # Function to get OpenAI response
8
  def get_openai_response(question):
9
  try:
10
+ api_key = os.environ["OPENAI_API_KEY"] # Access API key from environment variable
 
 
11
  llm = openai.OpenAI(
12
+ model_name="text-davinci-003", temperature=0.5, openai_api_key=api_key
13
  )
14
  with st.spinner("Generating response..."):
15
  response = llm(question)
 
18
  st.error(f"Error: {e}")
19
  return None
20
 
 
 
 
21
  # Initialize Streamlit app
22
  st.set_page_config(page_title="Enhanced Q&A Demo")
23
  st.header("Langchain App")
 
37
  if response:
38
  st.subheader("The answer is:")
39
  st.write(response)
40
+
41
  # Add response to history
42
  response_history.code(f"""
43
+ {input_text}
44
  **Answer:** {response}
45
  ---
46
  """)
47
  else:
48
  st.error("Something went wrong. Please try again later.")