Srinivasulu kethanaboina commited on
Commit
6097399
·
verified ·
1 Parent(s): cfa8dff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -27
app.py CHANGED
@@ -4,23 +4,21 @@ 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
11
- import uuid
12
  import random
 
 
 
 
 
 
13
 
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="meta-llama/Meta-Llama-3-8B-Instruct",
@@ -36,7 +34,7 @@ Settings.embed_model = HuggingFaceEmbedding(
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)
@@ -57,7 +55,7 @@ def handle_query(query):
57
  (
58
  "user",
59
  """
60
- You are the clara Redfernstech chatbot. Your goal is to provide accurate, professional, and helpful answers to user queries based on the company's data. Always ensure your responses are clear and concise. give response within 10-15 words only
61
  {context_str}
62
  Question:
63
  {query_str}
@@ -95,22 +93,20 @@ def handle_query(query):
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">
105
  <img src="https://rb.gy/8r06eg" alt="FernAi">
106
  </div>
107
  '''
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):
@@ -118,8 +114,8 @@ def chat_interface(message, history):
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
125
  message_data = {
@@ -129,9 +125,6 @@ def chat_interface(message, history):
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:
@@ -169,13 +162,14 @@ footer {
169
  color: #111;
170
  margin: 20px 0;
171
  }
172
-
173
  label.svelte-1b6s6s {display: none}
174
  div.svelte-rk35yg {display: none;}
175
  div.progress-text.svelte-z7cif2.meta-text {display: none;}
176
  '''
 
 
177
  gr.ChatInterface(chat_interface,
178
  css=css,
179
  description="Clara",
180
  clear_btn=None, undo_btn=None, retry_btn=None,
181
- ).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 random
8
+ import uuid
9
+ import datetime
10
+ from gradio_client import Client
11
+
12
+ # Initialize Gradio Client
13
+ client = Client("srinukethanaboina/SRUNU")
14
 
15
  def select_random_name():
16
  names = ['Clara', 'Lily']
17
  return random.choice(names)
18
 
 
19
  # Load environment variables
20
  load_dotenv()
21
+
 
 
22
  # Configure the Llama index settings
23
  Settings.llm = HuggingFaceInferenceAPI(
24
  model_name="meta-llama/Meta-Llama-3-8B-Instruct",
 
34
 
35
  # Define the directory for persistent storage and data
36
  PERSIST_DIR = "db"
37
+ PDF_DIRECTORY = 'data' # Directory containing PDFs
38
 
39
  # Ensure directories exist
40
  os.makedirs(PDF_DIRECTORY, exist_ok=True)
 
55
  (
56
  "user",
57
  """
58
+ You are the Clara Redfernstech chatbot. Your goal is to provide accurate, professional, and helpful answers to user queries based on the company's data. Always ensure your responses are clear and concise. give response within 10-15 words only
59
  {context_str}
60
  Question:
61
  {query_str}
 
93
  print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
94
  data_ingestion_from_directory()
95
 
96
+ def predict(message, history,request: gr.Request):
 
 
 
 
97
  logo_html = '''
98
  <div class="circle-logo">
99
  <img src="https://rb.gy/8r06eg" alt="FernAi">
100
  </div>
101
  '''
102
+ # Use the gradio_client API to process the chat history and IP address
103
+ response = client.predict(
104
+ ip_address=str(request.client.host), # Replace with actual IP address handling if needed
105
+ chat_history=message,
106
+ api_name="/predict"
107
+ )
108
  response_with_logo = f'<div class="response-with-logo">{logo_html}<div class="response-text">{response}</div></div>'
109
  return response_with_logo
 
 
 
110
 
111
  # Define your Gradio chat interface function (replace with your actual logic)
112
  def chat_interface(message, history):
 
114
  # Generate a unique session ID for this chat session
115
  session_id = str(uuid.uuid4())
116
 
117
+ # Process the user message and generate a response
118
+ response = predict(message, history)
119
 
120
  # Capture the message data
121
  message_data = {
 
125
  "timestamp": datetime.datetime.now().isoformat() # Use a library like datetime
126
  }
127
 
 
 
 
128
  # Return the bot response
129
  return response
130
  except Exception as e:
 
162
  color: #111;
163
  margin: 20px 0;
164
  }
 
165
  label.svelte-1b6s6s {display: none}
166
  div.svelte-rk35yg {display: none;}
167
  div.progress-text.svelte-z7cif2.meta-text {display: none;}
168
  '''
169
+
170
+ # Launch the Gradio interface
171
  gr.ChatInterface(chat_interface,
172
  css=css,
173
  description="Clara",
174
  clear_btn=None, undo_btn=None, retry_btn=None,
175
+ ).launch()