Spaces:
Runtime error
Runtime error
NCTCMumbai
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
|
2 |
-
"""
|
3 |
-
Credit to Derek Thomas, [email protected]
|
4 |
-
"""
|
5 |
from ragatouille import RAGPretrainedModel
|
6 |
import subprocess
|
7 |
-
|
|
|
|
|
8 |
import logging
|
9 |
from pathlib import Path
|
10 |
from time import perf_counter
|
@@ -31,15 +30,58 @@ env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
|
|
31 |
# Load the templates directly from the environment
|
32 |
template = env.get_template('template.j2')
|
33 |
template_html = env.get_template('template_html.j2')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
# crossEncoder
|
36 |
-
#cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
37 |
-
#cross_encoder = CrossEncoder('BAAI/bge-reranker-base')
|
38 |
# Examples
|
39 |
examples = ['what is social media and what are rules related to it for expenditure monitoring ',
|
40 |
'how many reports to be submitted by Expenditure observer with annexure names ?','what is expenditure limits for parlimentary constituency and assembly constituency'
|
41 |
]
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
def add_text(history, text):
|
45 |
history = [] if history is None else history
|
@@ -127,8 +169,13 @@ with gr.Blocks(theme='Insuz/SimpleIndigo') as demo:
|
|
127 |
<img src='logo.png' alt="Chatbot" width="50" height="50" />
|
128 |
</div>""",elem_id='heading')
|
129 |
gr.HTML(value="""<p style="font-family: sans-serif; font-size: 16px;">A free chat bot assistant for Expenditure Observers on Compendium on Election Expenditure Monitoring using Open source LLMs. <br> The bot can answer questions in natural language, taking relevant extracts from the ECI document which can be accessed <a href="https://www.eci.gov.in/eci-backend/public/api/download?url=LMAhAK6sOPBp%2FNFF0iRfXbEB1EVSLT41NNLRjYNJJP1KivrUxbfqkDatmHy12e%2Fzk1vx4ptJpQsKYHA87guoLjnPUWtHeZgKtEqs%2FyzfTTYIC0newOHHOjl1rl0u3mJBSIq%2Fi7zDsrcP74v%2FKr8UNw%3D%3D" style="color: #FF0000; text-decoration: none;"></a>.</p>""",elem_id='Sub-heading')
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
|
132 |
chatbot = gr.Chatbot(
|
133 |
[],
|
134 |
elem_id="chatbot",
|
@@ -153,14 +200,14 @@ with gr.Blocks(theme='Insuz/SimpleIndigo') as demo:
|
|
153 |
prompt_html = gr.HTML()
|
154 |
# Turn off interactivity while generating if you click
|
155 |
txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
156 |
-
bot, [chatbot, cross_encoder], [chatbot, prompt_html])
|
157 |
|
158 |
# Turn it back on
|
159 |
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
160 |
|
161 |
# Turn off interactivity while generating if you hit enter
|
162 |
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
163 |
-
bot, [chatbot, cross_encoder], [chatbot, prompt_html])
|
164 |
|
165 |
# Turn it back on
|
166 |
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
|
|
1 |
|
|
|
|
|
|
|
2 |
from ragatouille import RAGPretrainedModel
|
3 |
import subprocess
|
4 |
+
import json
|
5 |
+
import firebase_admin
|
6 |
+
from firebase_admin import credentials, firestore
|
7 |
import logging
|
8 |
from pathlib import Path
|
9 |
from time import perf_counter
|
|
|
30 |
# Load the templates directly from the environment
|
31 |
template = env.get_template('template.j2')
|
32 |
template_html = env.get_template('template_html.j2')
|
33 |
+
service_account_key='firebase.json'
|
34 |
+
# Create a Certificate object from the service account info
|
35 |
+
cred = credentials.Certificate(service_account_key)
|
36 |
+
# Initialize the Firebase Admin SDK
|
37 |
+
firebase_admin.initialize_app(cred)
|
38 |
+
|
39 |
+
# # Create a reference to the Firestore database
|
40 |
+
db = firestore.client()
|
41 |
|
|
|
|
|
|
|
42 |
# Examples
|
43 |
examples = ['what is social media and what are rules related to it for expenditure monitoring ',
|
44 |
'how many reports to be submitted by Expenditure observer with annexure names ?','what is expenditure limits for parlimentary constituency and assembly constituency'
|
45 |
]
|
46 |
+
#db usage
|
47 |
+
collection_name = 'Nirvachana' # Replace with your collection name
|
48 |
+
field_name = 'message_count' # Replace with your field name for count
|
49 |
+
|
50 |
+
|
51 |
+
def get_and_increment_value_count(db , collection_name, field_name):
|
52 |
+
"""
|
53 |
+
Retrieves a value count from the specified Firestore collection and field,
|
54 |
+
increments it by 1, and updates the field with the new value."""
|
55 |
+
collection_ref = db.collection(collection_name)
|
56 |
+
doc_ref = collection_ref.document('count_doc') # Assuming a dedicated document for count
|
57 |
+
|
58 |
+
# Use a transaction to ensure consistency across reads and writes
|
59 |
+
try:
|
60 |
+
with db.transaction() as transaction:
|
61 |
+
# Get the current value count (or initialize to 0 if it doesn't exist)
|
62 |
+
current_count_doc = doc_ref.get()
|
63 |
+
current_count_data = current_count_doc.to_dict()
|
64 |
+
if current_count_data:
|
65 |
+
current_count = current_count_data.get(field_name, 0)
|
66 |
+
else:
|
67 |
+
current_count = 0
|
68 |
+
# Increment the count
|
69 |
+
new_count = current_count + 1
|
70 |
+
# Update the document with the new count
|
71 |
+
transaction.set(doc_ref, {field_name: new_count})
|
72 |
+
return new_count
|
73 |
+
except Exception as e:
|
74 |
+
print(f"Error retrieving and updating value count: {e}")
|
75 |
+
return None # Indicate error
|
76 |
+
|
77 |
+
def update_count_html():
|
78 |
+
usage_count = get_and_increment_value_count(db ,collection_name, field_name)
|
79 |
+
count_html =gr.HTML(value= f"""
|
80 |
+
<div style="display: flex; justify-content: flex-end;">
|
81 |
+
<span style="font-weight: bold; color: maroon;">No of Usages:</span> {usage_count}
|
82 |
+
</div>
|
83 |
+
""")
|
84 |
+
return count_html
|
85 |
|
86 |
def add_text(history, text):
|
87 |
history = [] if history is None else history
|
|
|
169 |
<img src='logo.png' alt="Chatbot" width="50" height="50" />
|
170 |
</div>""",elem_id='heading')
|
171 |
gr.HTML(value="""<p style="font-family: sans-serif; font-size: 16px;">A free chat bot assistant for Expenditure Observers on Compendium on Election Expenditure Monitoring using Open source LLMs. <br> The bot can answer questions in natural language, taking relevant extracts from the ECI document which can be accessed <a href="https://www.eci.gov.in/eci-backend/public/api/download?url=LMAhAK6sOPBp%2FNFF0iRfXbEB1EVSLT41NNLRjYNJJP1KivrUxbfqkDatmHy12e%2Fzk1vx4ptJpQsKYHA87guoLjnPUWtHeZgKtEqs%2FyzfTTYIC0newOHHOjl1rl0u3mJBSIq%2Fi7zDsrcP74v%2FKr8UNw%3D%3D" style="color: #FF0000; text-decoration: none;"></a>.</p>""",elem_id='Sub-heading')
|
172 |
+
usage_count = get_and_increment_value_count(collection_name, field_name)
|
173 |
+
count_html =gr.HTML(value= f"""
|
174 |
+
<div style="display: flex; justify-content: flex-end;">
|
175 |
+
<span style="font-weight: bold; color: maroon;">No of Usages:</span> {usage_count}
|
176 |
+
</div>
|
177 |
+
""")
|
178 |
|
|
|
179 |
chatbot = gr.Chatbot(
|
180 |
[],
|
181 |
elem_id="chatbot",
|
|
|
200 |
prompt_html = gr.HTML()
|
201 |
# Turn off interactivity while generating if you click
|
202 |
txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
203 |
+
bot, [chatbot, cross_encoder], [chatbot, prompt_html]).then(update_count_html,[],[count_html])
|
204 |
|
205 |
# Turn it back on
|
206 |
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
207 |
|
208 |
# Turn off interactivity while generating if you hit enter
|
209 |
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
210 |
+
bot, [chatbot, cross_encoder], [chatbot, prompt_html]).then(update_count_html,[],[count_html])
|
211 |
|
212 |
# Turn it back on
|
213 |
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|