jasvir-singh1021 commited on
Commit
714614e
Β·
verified Β·
1 Parent(s): 0becbbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -19,17 +19,17 @@ if "conversation" not in st.session_state:
19
 
20
  # Sidebar settings
21
  with st.sidebar:
22
- st.title("βš™οΈ Settings")
23
- api_key = st.text_input("πŸ”‘ OpenAI API Key", type="password")
24
- temperature = st.slider("πŸ”₯ Temperature", 0.0, 1.0, 0.3, 0.1)
25
 
26
  # Main UI
27
- st.title("πŸ“„ Document Parser")
28
  st.markdown("Upload documents and ask questions using GPT.")
29
 
30
  # File uploader
31
  uploaded_files = st.file_uploader(
32
- "πŸ“€ Upload Documents (PDF, DOCX, TXT, HTML)",
33
  type=["pdf", "docx", "txt", "html"],
34
  accept_multiple_files=True
35
  )
@@ -50,11 +50,11 @@ def extract_text(file):
50
  return ""
51
 
52
  # Input field
53
- question = st.text_input("πŸ’¬ Ask a question about the uploaded documents:")
54
 
55
  # When "Ask" button is clicked
56
- if st.button("πŸš€ Ask") and uploaded_files and question and api_key:
57
- with st.spinner("🧠 Thinking..."):
58
 
59
  # Extract and combine text from all uploaded files
60
  combined_text = ""
@@ -62,12 +62,12 @@ if st.button("πŸš€ Ask") and uploaded_files and question and api_key:
62
  combined_text += extract_text(file) + "\n"
63
 
64
  if not combined_text.strip():
65
- st.warning("⚠️ Could not extract text from uploaded files.")
66
  else:
67
  try:
68
  openai.api_key = api_key
69
  response = openai.ChatCompletion.create(
70
- model="gpt-3.5-turbo", # βœ… Updated model
71
  messages=[
72
  {"role": "system", "content": "You are a helpful assistant that answers questions based on uploaded documents."},
73
  {"role": "user", "content": f"DOCUMENT:\n{combined_text[:6000]}\n\nQUESTION:\n{question}"}
@@ -81,11 +81,11 @@ if st.button("πŸš€ Ask") and uploaded_files and question and api_key:
81
  st.session_state.conversation.append({"role": "assistant", "content": answer})
82
 
83
  except Exception as e:
84
- st.error(f"❌ Error from OpenAI: {e}")
85
 
86
  # Display conversation
87
  if st.session_state.conversation:
88
- st.markdown("## 🧾 Conversation")
89
  for msg in st.session_state.conversation:
90
  st.markdown(f"**{'You' if msg['role'] == 'user' else 'Assistant'}:** {msg['content']}")
91
 
@@ -93,7 +93,7 @@ if st.session_state.conversation:
93
  col1, col2 = st.columns(2)
94
 
95
  with col1:
96
- if st.button("πŸ—‘οΈ Clear Conversation"):
97
  st.session_state.conversation = []
98
  st.experimental_rerun()
99
 
@@ -110,4 +110,4 @@ if st.session_state.conversation:
110
  mime = "application/json"
111
  filename = "conversation.json"
112
 
113
- st.download_button("πŸ“₯ Download", content, file_name=filename, mime=mime)
 
19
 
20
  # Sidebar settings
21
  with st.sidebar:
22
+ st.title("Settings")
23
+ api_key = st.text_input("OpenAI API Key", type="password")
24
+ temperature = st.slider("Temperature", 0.0, 1.0, 0.3, 0.1)
25
 
26
  # Main UI
27
+ st.title("Document Parser")
28
  st.markdown("Upload documents and ask questions using GPT.")
29
 
30
  # File uploader
31
  uploaded_files = st.file_uploader(
32
+ "Upload Documents (PDF, DOCX, TXT, HTML)",
33
  type=["pdf", "docx", "txt", "html"],
34
  accept_multiple_files=True
35
  )
 
50
  return ""
51
 
52
  # Input field
53
+ question = st.text_input("Ask a question about the uploaded documents:")
54
 
55
  # When "Ask" button is clicked
56
+ if st.button("Ask") and uploaded_files and question and api_key:
57
+ with st.spinner("Processing..."):
58
 
59
  # Extract and combine text from all uploaded files
60
  combined_text = ""
 
62
  combined_text += extract_text(file) + "\n"
63
 
64
  if not combined_text.strip():
65
+ st.warning("Could not extract text from uploaded files.")
66
  else:
67
  try:
68
  openai.api_key = api_key
69
  response = openai.ChatCompletion.create(
70
+ model="gpt-3.5-turbo",
71
  messages=[
72
  {"role": "system", "content": "You are a helpful assistant that answers questions based on uploaded documents."},
73
  {"role": "user", "content": f"DOCUMENT:\n{combined_text[:6000]}\n\nQUESTION:\n{question}"}
 
81
  st.session_state.conversation.append({"role": "assistant", "content": answer})
82
 
83
  except Exception as e:
84
+ st.error(f"Error from OpenAI: {e}")
85
 
86
  # Display conversation
87
  if st.session_state.conversation:
88
+ st.markdown("## Conversation")
89
  for msg in st.session_state.conversation:
90
  st.markdown(f"**{'You' if msg['role'] == 'user' else 'Assistant'}:** {msg['content']}")
91
 
 
93
  col1, col2 = st.columns(2)
94
 
95
  with col1:
96
+ if st.button("Clear Conversation"):
97
  st.session_state.conversation = []
98
  st.experimental_rerun()
99
 
 
110
  mime = "application/json"
111
  filename = "conversation.json"
112
 
113
+ st.download_button("Download", content, file_name=filename, mime=mime)