Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ enable_btn = gr.Button.update(interactive=True)
|
|
12 |
import sqlite3
|
13 |
import requests
|
14 |
|
15 |
-
def classify_vote(user_input):
|
16 |
url = "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"
|
17 |
headers = {
|
18 |
"accept": "*/*",
|
@@ -26,14 +26,16 @@ def classify_vote(user_input):
|
|
26 |
"multi_class": True
|
27 |
}
|
28 |
}
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
37 |
from pymongo.mongo_client import MongoClient
|
38 |
from pymongo.server_api import ServerApi
|
39 |
async def direct_regenerate(model, user_input, chatbot, character_name, character_description, user_name):
|
@@ -168,24 +170,40 @@ def update_ratings(state, winner_index, collection, category):
|
|
168 |
except Exception as e:
|
169 |
print(f"Error updating ratings: {str(e)}")
|
170 |
return [('Error', 'Failed to update ratings.'), ('Error', 'Failed to update ratings.')]
|
171 |
-
def vote_up_model(state, chatbot, chatbot2, character_name, character_description, user_name):
|
172 |
user_input = format_prompt(state, 0, character_name, character_description, user_name)
|
173 |
collection = init_database()
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
if top_category:
|
176 |
update_message = update_ratings(state, 0, collection, top_category)
|
177 |
chatbot.append(update_message[0])
|
178 |
chatbot2.append(update_message[1])
|
|
|
179 |
return chatbot, chatbot2, gr.Button.update(interactive=False, value="π Upvoted"), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
180 |
-
|
181 |
-
def vote_down_model(state, chatbot, chatbot2, character_name, character_description, user_name):
|
182 |
user_input = format_prompt(state, 1, character_name, character_description, user_name)
|
183 |
collection = init_database()
|
184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
if top_category:
|
186 |
update_message = update_ratings(state, 1, collection, top_category)
|
187 |
chatbot2.append(update_message[0])
|
188 |
chatbot.append(update_message[1])
|
|
|
189 |
return chatbot, chatbot2, gr.Button.update(interactive=False), gr.Button.update(interactive=False, value="π Upvoted"), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
190 |
|
191 |
async def user_ask(state, chatbot1, chatbot2, textbox, character_name, character_description, user_name):
|
@@ -367,9 +385,8 @@ with gr.Blocks() as demo:
|
|
367 |
textbox.submit(user_ask, inputs=[state, chatbot1, chatbot2, textbox, character_name, character_description, user_name], outputs=[state, chatbot1, chatbot2, textbox, upvote_btn_a, upvote_btn_b], queue=True)
|
368 |
collection = init_database()
|
369 |
|
370 |
-
upvote_btn_a.click(vote_up_model, inputs=[state, chatbot1, chatbot2, character_name, character_description, user_name], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
|
371 |
-
upvote_btn_b.click(vote_down_model, inputs=[state, chatbot1, chatbot2, character_name, character_description, user_name], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn])
|
372 |
-
|
373 |
# ...
|
374 |
with gr.Tab("π¬ Direct Chat"):
|
375 |
gr.Markdown("## π£οΈ Chat directly with a model!")
|
|
|
12 |
import sqlite3
|
13 |
import requests
|
14 |
|
15 |
+
async def classify_vote(user_input):
|
16 |
url = "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"
|
17 |
headers = {
|
18 |
"accept": "*/*",
|
|
|
26 |
"multi_class": True
|
27 |
}
|
28 |
}
|
29 |
+
async with aiohttp.ClientSession() as session:
|
30 |
+
async with session.post(url, headers=headers, json=payload) as response:
|
31 |
+
if response.status == 200:
|
32 |
+
response_data = await response.json()
|
33 |
+
top_category = response_data["labels"][0].strip()
|
34 |
+
return top_category
|
35 |
+
else:
|
36 |
+
print(f"Error: {response.status}")
|
37 |
+
return None
|
38 |
+
|
39 |
from pymongo.mongo_client import MongoClient
|
40 |
from pymongo.server_api import ServerApi
|
41 |
async def direct_regenerate(model, user_input, chatbot, character_name, character_description, user_name):
|
|
|
170 |
except Exception as e:
|
171 |
print(f"Error updating ratings: {str(e)}")
|
172 |
return [('Error', 'Failed to update ratings.'), ('Error', 'Failed to update ratings.')]
|
173 |
+
async def vote_up_model(state, chatbot, chatbot2, character_name, character_description, user_name):
|
174 |
user_input = format_prompt(state, 0, character_name, character_description, user_name)
|
175 |
collection = init_database()
|
176 |
+
|
177 |
+
# Disable both upvote buttons immediately
|
178 |
+
yield chatbot, chatbot2, gr.Button.update(interactive=False, value="π Upvoted"), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
179 |
+
|
180 |
+
# Process sentiment analysis asynchronously
|
181 |
+
sentiment_task = asyncio.create_task(classify_vote(user_input))
|
182 |
+
top_category = await sentiment_task
|
183 |
+
|
184 |
if top_category:
|
185 |
update_message = update_ratings(state, 0, collection, top_category)
|
186 |
chatbot.append(update_message[0])
|
187 |
chatbot2.append(update_message[1])
|
188 |
+
|
189 |
return chatbot, chatbot2, gr.Button.update(interactive=False, value="π Upvoted"), gr.Button.update(interactive=False), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
190 |
+
|
191 |
+
async def vote_down_model(state, chatbot, chatbot2, character_name, character_description, user_name):
|
192 |
user_input = format_prompt(state, 1, character_name, character_description, user_name)
|
193 |
collection = init_database()
|
194 |
+
|
195 |
+
# Disable both upvote buttons immediately
|
196 |
+
yield chatbot, chatbot2, gr.Button.update(interactive=False), gr.Button.update(interactive=False, value="π Upvoted"), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
197 |
+
|
198 |
+
# Process sentiment analysis asynchronously
|
199 |
+
sentiment_task = asyncio.create_task(classify_vote(user_input))
|
200 |
+
top_category = await sentiment_task
|
201 |
+
|
202 |
if top_category:
|
203 |
update_message = update_ratings(state, 1, collection, top_category)
|
204 |
chatbot2.append(update_message[0])
|
205 |
chatbot.append(update_message[1])
|
206 |
+
|
207 |
return chatbot, chatbot2, gr.Button.update(interactive=False), gr.Button.update(interactive=False, value="π Upvoted"), gr.Textbox.update(interactive=False), gr.Button.update(interactive=False)
|
208 |
|
209 |
async def user_ask(state, chatbot1, chatbot2, textbox, character_name, character_description, user_name):
|
|
|
385 |
textbox.submit(user_ask, inputs=[state, chatbot1, chatbot2, textbox, character_name, character_description, user_name], outputs=[state, chatbot1, chatbot2, textbox, upvote_btn_a, upvote_btn_b], queue=True)
|
386 |
collection = init_database()
|
387 |
|
388 |
+
upvote_btn_a.click(vote_up_model, inputs=[state, chatbot1, chatbot2, character_name, character_description, user_name], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn], api_name="upvote_a")
|
389 |
+
upvote_btn_b.click(vote_down_model, inputs=[state, chatbot1, chatbot2, character_name, character_description, user_name], outputs=[chatbot1, chatbot2, upvote_btn_a, upvote_btn_b, textbox, submit_btn], api_name="upvote_b")
|
|
|
390 |
# ...
|
391 |
with gr.Tab("π¬ Direct Chat"):
|
392 |
gr.Markdown("## π£οΈ Chat directly with a model!")
|