Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
import time
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
# Get the Hugging Face API Token from environment variables
|
|
@@ -26,23 +25,6 @@ def query_model(api_url, payload):
|
|
| 26 |
response = requests.post(api_url, headers=HEADERS, json=payload)
|
| 27 |
return response.json()
|
| 28 |
|
| 29 |
-
def count_tokens(text):
|
| 30 |
-
return len(text.split())
|
| 31 |
-
|
| 32 |
-
MAX_TOKENS_PER_MINUTE = 1000
|
| 33 |
-
token_count = 0
|
| 34 |
-
start_time = time.time()
|
| 35 |
-
|
| 36 |
-
def handle_token_limit(text):
|
| 37 |
-
global token_count, start_time
|
| 38 |
-
current_time = time.time()
|
| 39 |
-
if current_time - start_time > 60:
|
| 40 |
-
token_count = 0
|
| 41 |
-
start_time = current_time
|
| 42 |
-
token_count += count_tokens(text)
|
| 43 |
-
if token_count > MAX_TOKENS_PER_MINUTE:
|
| 44 |
-
raise ValueError("Token limit exceeded. Please wait before sending more messages.")
|
| 45 |
-
|
| 46 |
def add_message_to_conversation(user_message, bot_message, model_name):
|
| 47 |
st.session_state.conversation.append((user_message, bot_message, model_name))
|
| 48 |
|
|
@@ -74,7 +56,6 @@ question = st.text_input("Question", placeholder="Enter your question here...")
|
|
| 74 |
# Handle user input and LLM response
|
| 75 |
if st.button("Send") and question:
|
| 76 |
try:
|
| 77 |
-
handle_token_limit(question) # Check token limit before processing
|
| 78 |
with st.spinner("Waiting for the model to respond..."):
|
| 79 |
chat_history = " ".join(st.session_state.model_history[llm_selection]) + f"User: {question}\n"
|
| 80 |
if llm_selection == "Mistral-8x7B":
|
|
@@ -111,7 +92,6 @@ if st.button("Send") and question:
|
|
| 111 |
response = query_model(GEMMA_27B_IT_API_URL, {"inputs": chat_history})
|
| 112 |
answer = response.get("generated_text", "No response") if isinstance(response, dict) else response[0].get("generated_text", "No response") if isinstance(response, list) else "No response"
|
| 113 |
|
| 114 |
-
handle_token_limit(answer) # Check token limit for output
|
| 115 |
add_message_to_conversation(question, answer, llm_selection)
|
| 116 |
st.session_state.model_history[llm_selection].append(f"User: {question}\n{llm_selection}: {answer}\n")
|
| 117 |
except ValueError as e:
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
|
| 5 |
# Get the Hugging Face API Token from environment variables
|
|
|
|
| 25 |
response = requests.post(api_url, headers=HEADERS, json=payload)
|
| 26 |
return response.json()
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def add_message_to_conversation(user_message, bot_message, model_name):
|
| 29 |
st.session_state.conversation.append((user_message, bot_message, model_name))
|
| 30 |
|
|
|
|
| 56 |
# Handle user input and LLM response
|
| 57 |
if st.button("Send") and question:
|
| 58 |
try:
|
|
|
|
| 59 |
with st.spinner("Waiting for the model to respond..."):
|
| 60 |
chat_history = " ".join(st.session_state.model_history[llm_selection]) + f"User: {question}\n"
|
| 61 |
if llm_selection == "Mistral-8x7B":
|
|
|
|
| 92 |
response = query_model(GEMMA_27B_IT_API_URL, {"inputs": chat_history})
|
| 93 |
answer = response.get("generated_text", "No response") if isinstance(response, dict) else response[0].get("generated_text", "No response") if isinstance(response, list) else "No response"
|
| 94 |
|
|
|
|
| 95 |
add_message_to_conversation(question, answer, llm_selection)
|
| 96 |
st.session_state.model_history[llm_selection].append(f"User: {question}\n{llm_selection}: {answer}\n")
|
| 97 |
except ValueError as e:
|