Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,53 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
#load_dotenv() # Take environment variables from .env
|
6 |
|
7 |
import streamlit as st
|
8 |
-
import
|
9 |
|
10 |
-
#
|
|
|
11 |
|
|
|
12 |
def get_openai_response(question):
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
st.
|
23 |
-
|
24 |
-
|
25 |
-
#
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
if submit:
|
30 |
-
st.subheader("The response is")
|
31 |
-
response = get_openai_response(input)
|
32 |
-
st.write(response)
|
|
|
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
|
9 |
+
load_dotenv()
|
10 |
|
11 |
+
# Function to get OpenAI response
|
12 |
def get_openai_response(question):
|
13 |
+
try:
|
14 |
+
api_key = os.environ["OPENAI_API_KEY"]
|
15 |
+
llm = openai.OpenAI(
|
16 |
+
model_name="text-davinci-003", temperature=0.5, openai_api_key=api_key
|
17 |
+
)
|
18 |
+
with st.spinner("Generating response..."):
|
19 |
+
response = llm(question)
|
20 |
+
return response
|
21 |
+
except Exception as e:
|
22 |
+
st.error(f"Error: {e}")
|
23 |
+
return None
|
24 |
+
|
25 |
+
# Initialize Streamlit app
|
26 |
+
st.set_page_config(page_title="Enhanced Q&A Demo")
|
27 |
+
st.header("Langchain App")
|
28 |
+
|
29 |
+
# User input and submit button
|
30 |
+
input_text = st.text_input("Ask me anything:", key="input")
|
31 |
+
submit_button = st.button("Get Answer")
|
32 |
+
|
33 |
+
# Response history
|
34 |
+
response_history = st.empty()
|
35 |
+
|
36 |
+
# Handle button click and display response
|
37 |
+
if submit_button:
|
38 |
+
with st.spinner("Processing..."):
|
39 |
+
response = get_openai_response(input_text)
|
40 |
+
|
41 |
+
if response:
|
42 |
+
st.subheader("The answer is:")
|
43 |
+
st.write(response)
|
44 |
+
|
45 |
+
# Add response to history
|
46 |
+
response_history.code(f"""
|
47 |
+
{input_text}
|
48 |
+
**Answer:** {response}
|
49 |
+
---
|
50 |
+
""")
|
51 |
+
else:
|
52 |
+
st.error("Something went wrong. Please try again later.")
|
53 |
|
|
|
|
|
|
|
|