Tamil Eniyan commited on
Commit
ab64022
·
1 Parent(s): cd5fcc9

Updated app to use fire base

Browse files
Files changed (1) hide show
  1. app.py +26 -20
app.py CHANGED
@@ -1,23 +1,29 @@
 
 
1
  import streamlit as st
 
 
2
  import faiss
3
  import numpy as np
4
  import pickle
5
- import json
6
- import torch
7
  from sentence_transformers import SentenceTransformer
8
- from transformers import pipeline, RagTokenizer, RagRetriever, RagSequenceForGeneration
9
 
10
  # -----------------------------
11
  # Firebase Initialization
12
  # -----------------------------
13
- import firebase_admin
14
- from firebase_admin import credentials, firestore
15
-
16
  @st.cache_resource
17
  def init_firestore():
18
- # Replace with the path to your Firebase service account key
19
- cred = credentials.Certificate("path/to/serviceAccountKey.json")
20
- firebase_admin.initialize_app(cred)
 
 
 
 
 
 
 
21
  return firestore.client()
22
 
23
  db = init_firestore()
@@ -59,22 +65,21 @@ def load_curated_qa_pairs():
59
  try:
60
  with open(CURATED_QA_FILE, "r", encoding="utf-8") as f:
61
  return json.load(f)
62
- except:
 
63
  return []
64
 
65
  # ========================================
66
  # Chatbot Interface & Conversation Handling
67
  # ========================================
68
-
69
  def display_conversation():
70
  """Displays conversation history in a structured chat format."""
71
- for entry in st.session_state.conversation_history:
72
- role, message = entry
73
  with st.chat_message(role):
74
  st.write(message)
75
 
76
  def add_to_conversation(role, message):
77
- """Adds a message to conversation history."""
78
  st.session_state.conversation_history.append((role, message))
79
 
80
  # Initialize conversation history
@@ -99,20 +104,21 @@ user_query = st.chat_input("Ask a question about the document...")
99
  if user_query:
100
  add_to_conversation("user", user_query)
101
 
102
- # Check for curated Q/A pair
103
  answer = None
104
  for pair in curated_qa_pairs:
105
  if user_query.lower() in pair["question"].lower():
106
  answer = pair["answer"]
107
  break
108
 
109
- # If no curated answer found, save the question to Firebase and generate an answer.
110
  if not answer:
111
  try:
112
- db.collection('llmquestions').add({
113
- "question": user_query,
114
- "timestamp": firestore.SERVER_TIMESTAMP
115
- })
 
116
  except Exception as e:
117
  st.error(f"Error saving question to Firebase: {e}")
118
 
 
1
+ import os
2
+ import json
3
  import streamlit as st
4
+ import firebase_admin
5
+ from firebase_admin import credentials, firestore
6
  import faiss
7
  import numpy as np
8
  import pickle
 
 
9
  from sentence_transformers import SentenceTransformer
10
+ from transformers import pipeline
11
 
12
  # -----------------------------
13
  # Firebase Initialization
14
  # -----------------------------
 
 
 
15
  @st.cache_resource
16
  def init_firestore():
17
+ # Load the service account JSON from the environment variable
18
+ firebase_creds_json = os.environ.get("FIREBASE_SERVICE_ACCOUNT")
19
+ if not firebase_creds_json:
20
+ st.error("Firebase service account credentials not found in environment variables!")
21
+ return None
22
+ service_account_info = json.loads(firebase_creds_json)
23
+ cred = credentials.Certificate(service_account_info)
24
+ # Initialize the Firebase app only once
25
+ if not firebase_admin._apps:
26
+ firebase_admin.initialize_app(cred)
27
  return firestore.client()
28
 
29
  db = init_firestore()
 
65
  try:
66
  with open(CURATED_QA_FILE, "r", encoding="utf-8") as f:
67
  return json.load(f)
68
+ except Exception as e:
69
+ st.error(f"Error loading curated Q/A pairs: {e}")
70
  return []
71
 
72
  # ========================================
73
  # Chatbot Interface & Conversation Handling
74
  # ========================================
 
75
  def display_conversation():
76
  """Displays conversation history in a structured chat format."""
77
+ for role, message in st.session_state.conversation_history:
 
78
  with st.chat_message(role):
79
  st.write(message)
80
 
81
  def add_to_conversation(role, message):
82
+ """Adds a message to the conversation history."""
83
  st.session_state.conversation_history.append((role, message))
84
 
85
  # Initialize conversation history
 
104
  if user_query:
105
  add_to_conversation("user", user_query)
106
 
107
+ # Check for a curated Q/A pair match
108
  answer = None
109
  for pair in curated_qa_pairs:
110
  if user_query.lower() in pair["question"].lower():
111
  answer = pair["answer"]
112
  break
113
 
114
+ # If no curated answer is found, save the question to Firebase and process it.
115
  if not answer:
116
  try:
117
+ if db is not None:
118
+ db.collection("llmquestions").add({
119
+ "question": user_query,
120
+ "timestamp": firestore.SERVER_TIMESTAMP
121
+ })
122
  except Exception as e:
123
  st.error(f"Error saving question to Firebase: {e}")
124