shukdevdatta123 commited on
Commit
5393bbb
·
verified ·
1 Parent(s): 9aba39a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -39,9 +39,8 @@ def get_relevant_context(pdf_text, query, num_contexts=3):
39
  return relevant_context
40
 
41
  # Function to generate a response from GPT-4 chat model
42
- def generate_response(context, question):
43
- messages = [
44
- {"role": "system", "content": "You are a helpful assistant expert on GPT-4."},
45
  {"role": "user", "content": f"Context: {context}\nQuestion: {question}"}
46
  ]
47
  response = openai.ChatCompletion.create(
@@ -50,7 +49,11 @@ def generate_response(context, question):
50
  max_tokens=1200,
51
  temperature=0.7,
52
  )
53
- return response['choices'][0]['message']['content'].strip()
 
 
 
 
54
 
55
  # Function to handle irrelevant questions
56
  def is_irrelevant_question(question):
@@ -77,6 +80,10 @@ def main():
77
  pdf_text = extract_pdf_text(pdf_file)
78
  st.write("PDF content loaded successfully!")
79
 
 
 
 
 
80
  # User input: the question they want to ask
81
  question = st.text_input("Ask your question:")
82
 
@@ -89,10 +96,21 @@ def main():
89
  relevant_context = get_relevant_context(pdf_text, question)
90
 
91
  # Generate the response from GPT-4 chat model
92
- answer = generate_response(relevant_context, question)
 
 
 
93
 
94
  # Display the answer
95
  st.write(f"Answer: {answer}")
 
 
 
 
 
 
 
 
96
  else:
97
  st.warning("Please enter your OpenAI API Key to use the chatbot.")
98
 
 
39
  return relevant_context
40
 
41
  # Function to generate a response from GPT-4 chat model
42
+ def generate_response(context, question, conversation_history):
43
+ messages = conversation_history + [
 
44
  {"role": "user", "content": f"Context: {context}\nQuestion: {question}"}
45
  ]
46
  response = openai.ChatCompletion.create(
 
49
  max_tokens=1200,
50
  temperature=0.7,
51
  )
52
+ answer = response['choices'][0]['message']['content'].strip()
53
+
54
+ # Append the new answer to the conversation history
55
+ conversation_history.append({"role": "assistant", "content": answer})
56
+ return answer, conversation_history
57
 
58
  # Function to handle irrelevant questions
59
  def is_irrelevant_question(question):
 
80
  pdf_text = extract_pdf_text(pdf_file)
81
  st.write("PDF content loaded successfully!")
82
 
83
+ # Initialize conversation history (this will persist between interactions)
84
+ if 'conversation_history' not in st.session_state:
85
+ st.session_state.conversation_history = []
86
+
87
  # User input: the question they want to ask
88
  question = st.text_input("Ask your question:")
89
 
 
96
  relevant_context = get_relevant_context(pdf_text, question)
97
 
98
  # Generate the response from GPT-4 chat model
99
+ answer, conversation_history = generate_response(relevant_context, question, st.session_state.conversation_history)
100
+
101
+ # Update the conversation history in session state
102
+ st.session_state.conversation_history = conversation_history
103
 
104
  # Display the answer
105
  st.write(f"Answer: {answer}")
106
+
107
+ # End conversation button to reset chat history
108
+ if st.button("END CONVERSATION"):
109
+ st.session_state.conversation_history = [] # Reset conversation history
110
+ st.write("Conversation has been reset. Feel free to ask new questions.")
111
+
112
+ else:
113
+ st.warning("Please upload a PDF file to proceed.")
114
  else:
115
  st.warning("Please enter your OpenAI API Key to use the chatbot.")
116