taimoor61 commited on
Commit
05cbd63
·
1 Parent(s): 1aaf699

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -26
app.py CHANGED
@@ -1,32 +1,53 @@
1
- # Q&A Chatbot
2
- from langchain.llms import openai
3
- #from dotenv import load_dotenv
4
-
5
- #load_dotenv() # Take environment variables from .env
6
 
7
  import streamlit as st
8
- import os
9
 
10
- #Function to load OpenAI model and get response
 
11
 
 
12
  def get_openai_response(question):
13
- llm = openai.OpenAI(model_name= 'text-davinci-003',
14
- temperature = 0.5)
15
- response = llm(question)
16
- return response
17
-
18
- ## Initialize our Streamlit Apps
19
-
20
- st.set_page_config(page_title="Q&A Demo")
21
-
22
- st.header("Langchain Application")
23
-
24
- input = st.text_input("Input: ",key='input')
25
- #response = get_openai_response(input)
26
-
27
- submit = st.button("Ask the question")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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