Spaces:
Build error
Build error
Update app.py
Browse files12345619 unmodified lines262728293031323334353637383940414243444546474825 unmodified lines7475767778798030 unmodified lines111112113114115116117import osimport requestsimport timeimport streamlit as st# Get the Hugging Face API Token from environment variables19 unmodified linesresponse = requests.post(api_url, headers=HEADERS, json=payload)return response.json()def count_tokens(text):return len(text.split())MAX_TOKENS_PER_MINUTE = 1000token_count = 0start_time = time.time()def handle_token_limit(text):global token_count, start_timecurrent_time = time.time()if current_time - start_time > 60:token_count = 0start_time = current_timetoken_count += count_tokens(text)if token_count > MAX_TOKENS_PER_MINUTE:raise ValueError("Token limit exceeded. Please wait before sending more messages.")def add_message_to_conversation(user_message, bot_message, model_name):st.session_state.conversation.append((user_message, bot_message, model_name))25 unmodified lines# Handle user input and LLM responseif st.button("Send") and question:try:handle_token_limit(question) # Check token limit before processingwith st.spinner("Waiting for the model to respond..."):chat_history = " ".join(st.session_state.model_history[llm_selection]) + f"User: {question}\n"if llm_selection == "Mistral-8x7B":30 unmodified linesresponse = query_model(GEMMA_27B_IT_API_URL, {"inputs": chat_history})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"handle_token_limit(answer) # Check token limit for outputadd_message_to_conversation(question, answer, llm_selection)st.session_state.model_history[llm_selection].append(f"User: {question}\n{llm_selection}: {answer}\n")except ValueError as e:1234519 unmodified lines25262728293025 unmodified lines56575859606130 unmodified lines929394959697import osimport requestsimport streamlit as st# Get the Hugging Face API Token from environment variables19 unmodified linesresponse = requests.post(api_url, headers=HEADERS, json=payload)return response.json()def add_message_to_conversation(user_message, bot_message, model_name):st.session_state.conversation.append((user_message, bot_message, model_name))25 unmodified lines# Handle user input and LLM responseif st.button("Send") and question:try:with st.spinner("Waiting for the model to respond..."):chat_history = " ".join(st.session_state.model_history[llm_selection]) + f"User: {question}\n"if llm_selection == "Mistral-8x7B":30 unmodified linesresponse = query_model(GEMMA_27B_IT_API_URL, {"inputs": chat_history})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"add_message_to_conversation(question, answer, llm_selection)st.session_state.model_history[llm_selection].append(f"User: {question}\n{llm_selection}: {answer}\n")except ValueError as e:
app.py-20