srinuksv commited on
Commit
5762724
1 Parent(s): 0f838ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -46
app.py CHANGED
@@ -4,7 +4,6 @@ import os
4
  from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
5
  from llama_index.llms.huggingface import HuggingFaceInferenceAPI
6
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
7
- from sentence_transformers import SentenceTransformer
8
  import firebase_admin
9
  from firebase_admin import db, credentials
10
  import datetime
@@ -15,13 +14,14 @@ def select_random_name():
15
  names = ['Clara', 'Lily']
16
  return random.choice(names)
17
 
18
- # Example usage
19
  # Load environment variables
20
  load_dotenv()
21
- # authenticate to firebase
 
22
  cred = credentials.Certificate("redfernstech-fd8fe-firebase-adminsdk-g9vcn-0537b4efd6.json")
23
  firebase_admin.initialize_app(cred, {"databaseURL": "https://redfernstech-fd8fe-default-rtdb.firebaseio.com/"})
24
- # Configure the Llama index settings
 
25
  Settings.llm = HuggingFaceInferenceAPI(
26
  model_name="facebook/rag-token-nq",
27
  tokenizer_name="facebook/rag-token-nq",
@@ -34,9 +34,9 @@ Settings.embed_model = HuggingFaceEmbedding(
34
  model_name="BAAI/bge-small-en-v1.5"
35
  )
36
 
37
- # Define the directory for persistent storage and data
38
  PERSIST_DIR = "db"
39
- PDF_DIRECTORY = 'data' # Changed to the directory containing PDFs
40
 
41
  # Ensure directories exist
42
  os.makedirs(PDF_DIRECTORY, exist_ok=True)
@@ -46,7 +46,7 @@ os.makedirs(PERSIST_DIR, exist_ok=True)
46
  current_chat_history = []
47
 
48
  def data_ingestion_from_directory():
49
- # Use SimpleDirectoryReader on the directory containing the PDF files
50
  documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
51
  storage_context = StorageContext.from_defaults()
52
  index = VectorStoreIndex.from_documents(documents)
@@ -91,14 +91,6 @@ def handle_query(query):
91
 
92
  return response
93
 
94
- # Example usage: Process PDF ingestion from directory
95
- print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
96
- data_ingestion_from_directory()
97
-
98
- # Define the function to handle predictions
99
- """def predict(message,history):
100
- response = handle_query(message)
101
- return response"""
102
  def predict(message, history):
103
  logo_html = '''
104
  <div class="circle-logo">
@@ -108,17 +100,17 @@ def predict(message, history):
108
  response = handle_query(message)
109
  response_with_logo = f'<div class="response-with-logo">{logo_html}<div class="response-text">{response}</div></div>'
110
  return response_with_logo
 
111
  def save_chat_message(session_id, message_data):
112
- ref = db.reference(f'/chat_history/{session_id}') # Use the session ID to save chat data
113
  ref.push().set(message_data)
114
 
115
- # Define your Gradio chat interface function (replace with your actual logic)
116
  def chat_interface(message, history):
117
  try:
118
  # Generate a unique session ID for this chat session
119
  session_id = str(uuid.uuid4())
120
 
121
- # Process the user message and generate a response (your chatbot logic)
122
  response = handle_query(message)
123
 
124
  # Capture the message data
@@ -126,13 +118,12 @@ def chat_interface(message, history):
126
  "sender": "user",
127
  "message": message,
128
  "response": response,
129
- "timestamp": datetime.datetime.now().isoformat() # Use a library like datetime
130
  }
131
 
132
- # Call the save function to store in Firebase with the generated session ID
133
  save_chat_message(session_id, message_data)
134
 
135
- # Return the bot response
136
  return response
137
  except Exception as e:
138
  return str(e)
@@ -140,32 +131,34 @@ def chat_interface(message, history):
140
  # Custom CSS for styling
141
  css = '''
142
  .circle-logo {
143
- display: inline-block;
144
- width: 40px;
145
- height: 40px;
146
- border-radius: 50%;
147
- overflow: hidden;
148
- margin-right: 10px;
149
- vertical-align: middle;
150
- }
151
- .circle-logo img {
152
- width: 100%;
153
- height: 100%;
154
- object-fit: cover;
155
- }
156
- .response-with-logo {
157
- display: flex;
158
- align-items: center;
159
- margin-bottom: 10px;
160
- }
161
- footer {
162
  display: none !important;
163
  background-color: #F8D7DA;
164
  }
165
- label.svelte-1b6s6s {display: none}
166
  '''
167
- gr.ChatInterface(chat_interface,
168
- css=css,
169
- description="Clara",
170
- clear_btn=None, undo_btn=None, retry_btn=None,
171
- ).launch()
 
 
 
4
  from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
5
  from llama_index.llms.huggingface import HuggingFaceInferenceAPI
6
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
 
7
  import firebase_admin
8
  from firebase_admin import db, credentials
9
  import datetime
 
14
  names = ['Clara', 'Lily']
15
  return random.choice(names)
16
 
 
17
  # Load environment variables
18
  load_dotenv()
19
+
20
+ # Authenticate to Firebase
21
  cred = credentials.Certificate("redfernstech-fd8fe-firebase-adminsdk-g9vcn-0537b4efd6.json")
22
  firebase_admin.initialize_app(cred, {"databaseURL": "https://redfernstech-fd8fe-default-rtdb.firebaseio.com/"})
23
+
24
+ # Configure Llama index settings
25
  Settings.llm = HuggingFaceInferenceAPI(
26
  model_name="facebook/rag-token-nq",
27
  tokenizer_name="facebook/rag-token-nq",
 
34
  model_name="BAAI/bge-small-en-v1.5"
35
  )
36
 
37
+ # Define directories for storage and data
38
  PERSIST_DIR = "db"
39
+ PDF_DIRECTORY = 'data' # Directory containing PDFs
40
 
41
  # Ensure directories exist
42
  os.makedirs(PDF_DIRECTORY, exist_ok=True)
 
46
  current_chat_history = []
47
 
48
  def data_ingestion_from_directory():
49
+ # Use SimpleDirectoryReader on the directory containing PDF files
50
  documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
51
  storage_context = StorageContext.from_defaults()
52
  index = VectorStoreIndex.from_documents(documents)
 
91
 
92
  return response
93
 
 
 
 
 
 
 
 
 
94
  def predict(message, history):
95
  logo_html = '''
96
  <div class="circle-logo">
 
100
  response = handle_query(message)
101
  response_with_logo = f'<div class="response-with-logo">{logo_html}<div class="response-text">{response}</div></div>'
102
  return response_with_logo
103
+
104
  def save_chat_message(session_id, message_data):
105
+ ref = db.reference(f'/chat_history/{session_id}')
106
  ref.push().set(message_data)
107
 
 
108
  def chat_interface(message, history):
109
  try:
110
  # Generate a unique session ID for this chat session
111
  session_id = str(uuid.uuid4())
112
 
113
+ # Process the user message and generate a response
114
  response = handle_query(message)
115
 
116
  # Capture the message data
 
118
  "sender": "user",
119
  "message": message,
120
  "response": response,
121
+ "timestamp": datetime.datetime.now().isoformat()
122
  }
123
 
124
+ # Save the chat message to Firebase
125
  save_chat_message(session_id, message_data)
126
 
 
127
  return response
128
  except Exception as e:
129
  return str(e)
 
131
  # Custom CSS for styling
132
  css = '''
133
  .circle-logo {
134
+ display: inline-block;
135
+ width: 40px;
136
+ height: 40px;
137
+ border-radius: 50%;
138
+ overflow: hidden;
139
+ margin-right: 10px;
140
+ vertical-align: middle;
141
+ }
142
+ .circle-logo img {
143
+ width: 100%;
144
+ height: 100%;
145
+ object-fit: cover;
146
+ }
147
+ .response-with-logo {
148
+ display: flex;
149
+ align-items: center;
150
+ margin-bottom: 10px;
151
+ }
152
+ footer {
153
  display: none !important;
154
  background-color: #F8D7DA;
155
  }
156
+ label.svelte-1b6s6s {display: none}
157
  '''
158
+
159
+ gr.ChatInterface(
160
+ chat_interface,
161
+ css=css,
162
+ description="Clara",
163
+ clear_btn=None, undo_btn=None, retry_btn=None
164
+ ).launch()