Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import time
|
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
# Get the Hugging Face API Token from environment variables
|
| 7 |
-
HF_API_TOKEN = os.getenv("
|
| 8 |
if not HF_API_TOKEN:
|
| 9 |
raise ValueError("Hugging Face API Token is not set in the environment variables.")
|
| 10 |
|
|
@@ -13,49 +13,22 @@ MISTRAL_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral
|
|
| 13 |
MINICHAT_API_URL = "https://api-inference.huggingface.co/models/GeneZC/MiniChat-2-3B"
|
| 14 |
DIALOGPT_API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-large"
|
| 15 |
PHI3_API_URL = "https://api-inference.huggingface.co/models/microsoft/Phi-3-mini-4k-instruct"
|
|
|
|
|
|
|
| 16 |
META_LLAMA_70B_API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct"
|
| 17 |
META_LLAMA_8B_API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
|
| 18 |
GEMMA_27B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-2-27b"
|
| 19 |
GEMMA_27B_IT_API_URL = "https://api-inference.huggingface.co/models/google/gemma-2-27b-it"
|
| 20 |
-
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 21 |
-
|
| 22 |
-
def query_mistral(payload):
|
| 23 |
-
response = requests.post(MISTRAL_API_URL, headers=HEADERS, json=payload)
|
| 24 |
-
st.write(f"Mistral API response: {response.json()}") # Debugging log
|
| 25 |
-
return response.json()
|
| 26 |
-
|
| 27 |
-
def query_minichat(payload):
|
| 28 |
-
response = requests.post(MINICHAT_API_URL, headers=HEADERS, json=payload)
|
| 29 |
-
return response.json()
|
| 30 |
-
|
| 31 |
-
def query_dialogpt(payload):
|
| 32 |
-
response = requests.post(DIALOGPT_API_URL, headers=HEADERS, json=payload)
|
| 33 |
-
return response.json()
|
| 34 |
|
| 35 |
-
|
| 36 |
-
response = requests.post(PHI3_API_URL, headers=HEADERS, json=payload)
|
| 37 |
-
return response.json()
|
| 38 |
-
|
| 39 |
-
def query_meta_llama_70b(payload):
|
| 40 |
-
response = requests.post(META_LLAMA_70B_API_URL, headers=HEADERS, json=payload)
|
| 41 |
-
return response.json()
|
| 42 |
-
|
| 43 |
-
def query_meta_llama_8b(payload):
|
| 44 |
-
response = requests.post(META_LLAMA_8B_API_URL, headers=HEADERS, json=payload)
|
| 45 |
-
return response.json()
|
| 46 |
-
|
| 47 |
-
def query_gemma_27b(payload):
|
| 48 |
-
response = requests.post(GEMMA_27B_API_URL, headers=HEADERS, json=payload)
|
| 49 |
-
return response.json()
|
| 50 |
|
| 51 |
-
def
|
| 52 |
-
response = requests.post(
|
| 53 |
return response.json()
|
| 54 |
|
| 55 |
def count_tokens(text):
|
| 56 |
return len(text.split())
|
| 57 |
|
| 58 |
-
# Token limit handling
|
| 59 |
MAX_TOKENS_PER_MINUTE = 1000
|
| 60 |
token_count = 0
|
| 61 |
start_time = time.time()
|
|
@@ -76,16 +49,24 @@ def add_message_to_conversation(user_message, bot_message, model_name):
|
|
| 76 |
# Streamlit app
|
| 77 |
st.set_page_config(page_title="Multi-LLM Chatbot Interface", layout="wide")
|
| 78 |
st.title("Multi-LLM Chatbot Interface")
|
| 79 |
-
st.write("Multi LLM-Chatbot Interface
|
| 80 |
|
| 81 |
# Initialize session state for conversation and model history
|
| 82 |
if "conversation" not in st.session_state:
|
| 83 |
st.session_state.conversation = []
|
| 84 |
if "model_history" not in st.session_state:
|
| 85 |
-
st.session_state.model_history = {model: [] for model in [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Dropdown for LLM selection
|
| 88 |
-
llm_selection = st.selectbox("Select Language Model", [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# User input for question
|
| 91 |
question = st.text_input("Question", placeholder="Enter your question here...")
|
|
@@ -93,85 +74,46 @@ question = st.text_input("Question", placeholder="Enter your question here...")
|
|
| 93 |
# Handle user input and LLM response
|
| 94 |
if st.button("Send") and question:
|
| 95 |
try:
|
| 96 |
-
handle_token_limit(question)
|
| 97 |
with st.spinner("Waiting for the model to respond..."):
|
| 98 |
chat_history = " ".join(st.session_state.model_history[llm_selection]) + f"User: {question}\n"
|
| 99 |
if llm_selection == "Mistral-8x7B":
|
| 100 |
-
|
| 101 |
-
if isinstance(
|
| 102 |
-
mistral_answer = mistral_response[0].get("generated_text", "No response")
|
| 103 |
-
else:
|
| 104 |
-
mistral_answer = "No response"
|
| 105 |
-
add_message_to_conversation(question, mistral_answer, llm_selection)
|
| 106 |
-
st.session_state.model_history[llm_selection].append(f"User: {question}\nMistral-8x7B: {mistral_answer}\n")
|
| 107 |
-
elif llm_selection == "Meta-Llama-3-70B-Instruct":
|
| 108 |
-
meta_llama_70b_response = query_meta_llama_70b({"inputs": chat_history})
|
| 109 |
-
if isinstance(meta_llama_70b_response, dict) and "generated_text" in meta_llama_70b_response:
|
| 110 |
-
meta_llama_70b_answer = meta_llama_70b_response["generated_text"]
|
| 111 |
-
elif isinstance(meta_llama_70b_response, list) and len(meta_llama_70b_response) > 0:
|
| 112 |
-
meta_llama_70b_answer = meta_llama_70b_response[0].get("generated_text", "No response")
|
| 113 |
-
else:
|
| 114 |
-
meta_llama_70b_answer = "No response"
|
| 115 |
-
add_message_to_conversation(question, meta_llama_70b_answer, llm_selection)
|
| 116 |
-
st.session_state.model_history[llm_selection].append(f"User: {question}\nMeta-Llama-3-70B-Instruct: {meta_llama_70b_answer}\n")
|
| 117 |
-
elif llm_selection == "Meta-Llama-3-8B-Instruct":
|
| 118 |
-
meta_llama_8b_response = query_meta_llama_8b({"inputs": chat_history})
|
| 119 |
-
if isinstance(meta_llama_8b_response, dict) and "generated_text" in meta_llama_8b_response:
|
| 120 |
-
meta_llama_8b_answer = meta_llama_8b_response["generated_text"]
|
| 121 |
-
elif isinstance(meta_llama_8b_response, list) and len(meta_llama_8b_response) > 0:
|
| 122 |
-
meta_llama_8b_answer = meta_llama_8b_response[0].get("generated_text", "No response")
|
| 123 |
-
else:
|
| 124 |
-
meta_llama_8b_answer = "No response"
|
| 125 |
-
add_message_to_conversation(question, meta_llama_8b_answer, llm_selection)
|
| 126 |
-
st.session_state.model_history[llm_selection].append(f"User: {question}\nMeta-Llama-3-8B-Instruct: {meta_llama_8b_answer}\n")
|
| 127 |
elif llm_selection == "MiniChat-2-3B":
|
| 128 |
-
|
| 129 |
-
if "error" in
|
| 130 |
-
|
| 131 |
-
elif isinstance(minichat_response, list) and len(minichat_response) > 0:
|
| 132 |
-
minichat_answer = minichat_response[0].get("generated_text", "No response")
|
| 133 |
else:
|
| 134 |
-
|
| 135 |
-
add_message_to_conversation(question, minichat_answer, llm_selection)
|
| 136 |
-
st.session_state.model_history[llm_selection].append(f"User: {question}\nMiniChat-2-3B: {minichat_answer}\n")
|
| 137 |
elif llm_selection == "DialoGPT (GPT-2-1.5B)":
|
| 138 |
-
|
| 139 |
-
if isinstance(
|
| 140 |
-
dialogpt_answer = dialogpt_response["generated_text"]
|
| 141 |
-
elif isinstance(dialogpt_response, list) and len(dialogpt_response) > 0:
|
| 142 |
-
dialogpt_answer = dialogpt_response[0].get("generated_text", "No response")
|
| 143 |
-
else:
|
| 144 |
-
dialogpt_answer = "No response"
|
| 145 |
-
add_message_to_conversation(question, dialogpt_answer, llm_selection)
|
| 146 |
-
st.session_state.model_history[llm_selection].append(f"User: {question}\nDialoGPT (GPT-2-1.5B): {dialogpt_answer}\n")
|
| 147 |
elif llm_selection == "Phi-3-mini-4k-instruct":
|
| 148 |
-
|
| 149 |
-
if isinstance(
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
elif llm_selection == "Gemma-2-27B":
|
| 156 |
-
|
| 157 |
-
if isinstance(
|
| 158 |
-
gemma_answer = gemma_response["generated_text"]
|
| 159 |
-
elif isinstance(gemma_response, list) and len(gemma_response) > 0:
|
| 160 |
-
gemma_answer = gemma_response[0].get("generated_text", "No response")
|
| 161 |
-
else:
|
| 162 |
-
gemma_answer = "No response"
|
| 163 |
-
add_message_to_conversation(question, gemma_answer, llm_selection)
|
| 164 |
-
st.session_state.model_history[llm_selection].append(f"User: {question}\nGemma-2-27B: {gemma_answer}\n")
|
| 165 |
elif llm_selection == "Gemma-2-27B-IT":
|
| 166 |
-
|
| 167 |
-
if isinstance(
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
gemma_27b_it_answer = "No response"
|
| 173 |
-
add_message_to_conversation(question, gemma_27b_it_answer, llm_selection)
|
| 174 |
-
st.session_state.model_history[llm_selection].append(f"User: {question}\nGemma-2-27B-IT: {gemma_27b_it_answer}\n")
|
| 175 |
except ValueError as e:
|
| 176 |
st.error(str(e))
|
| 177 |
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
# Get the Hugging Face API Token from environment variables
|
| 7 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 8 |
if not HF_API_TOKEN:
|
| 9 |
raise ValueError("Hugging Face API Token is not set in the environment variables.")
|
| 10 |
|
|
|
|
| 13 |
MINICHAT_API_URL = "https://api-inference.huggingface.co/models/GeneZC/MiniChat-2-3B"
|
| 14 |
DIALOGPT_API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-large"
|
| 15 |
PHI3_API_URL = "https://api-inference.huggingface.co/models/microsoft/Phi-3-mini-4k-instruct"
|
| 16 |
+
GEMMA_API_URL = "https://api-inference.huggingface.co/models/google/gemma-1.1-7b-it"
|
| 17 |
+
GEMMA_2B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-1.1-2b-it"
|
| 18 |
META_LLAMA_70B_API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct"
|
| 19 |
META_LLAMA_8B_API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
|
| 20 |
GEMMA_27B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-2-27b"
|
| 21 |
GEMMA_27B_IT_API_URL = "https://api-inference.huggingface.co/models/google/gemma-2-27b-it"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
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()
|
|
|
|
| 49 |
# Streamlit app
|
| 50 |
st.set_page_config(page_title="Multi-LLM Chatbot Interface", layout="wide")
|
| 51 |
st.title("Multi-LLM Chatbot Interface")
|
| 52 |
+
st.write("Multi LLM-Chatbot Interface")
|
| 53 |
|
| 54 |
# Initialize session state for conversation and model history
|
| 55 |
if "conversation" not in st.session_state:
|
| 56 |
st.session_state.conversation = []
|
| 57 |
if "model_history" not in st.session_state:
|
| 58 |
+
st.session_state.model_history = {model: [] for model in [
|
| 59 |
+
"Mistral-8x7B", "MiniChat-2-3B", "DialoGPT (GPT-2-1.5B)", "Phi-3-mini-4k-instruct",
|
| 60 |
+
"Gemma-1.1-7B", "Gemma-1.1-2B", "Meta-Llama-3-70B-Instruct", "Meta-Llama-3-8B-Instruct",
|
| 61 |
+
"Gemma-2-27B", "Gemma-2-27B-IT"
|
| 62 |
+
]}
|
| 63 |
|
| 64 |
# Dropdown for LLM selection
|
| 65 |
+
llm_selection = st.selectbox("Select Language Model", [
|
| 66 |
+
"Mistral-8x7B", "MiniChat-2-3B", "DialoGPT (GPT-2-1.5B)", "Phi-3-mini-4k-instruct",
|
| 67 |
+
"Gemma-1.1-7B", "Gemma-1.1-2B", "Meta-Llama-3-70B-Instruct", "Meta-Llama-3-8B-Instruct",
|
| 68 |
+
"Gemma-2-27B", "Gemma-2-27B-IT"
|
| 69 |
+
])
|
| 70 |
|
| 71 |
# User input for question
|
| 72 |
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":
|
| 81 |
+
response = query_model(MISTRAL_API_URL, {"inputs": chat_history})
|
| 82 |
+
answer = response[0].get("generated_text", "No response") if isinstance(response, list) else "No response"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
elif llm_selection == "MiniChat-2-3B":
|
| 84 |
+
response = query_model(MINICHAT_API_URL, {"inputs": chat_history})
|
| 85 |
+
if "error" in response and "is currently loading" in response["error"]:
|
| 86 |
+
answer = f"Model is loading, please wait {response['estimated_time']} seconds."
|
|
|
|
|
|
|
| 87 |
else:
|
| 88 |
+
answer = response[0].get("generated_text", "No response") if isinstance(response, list) else "No response"
|
|
|
|
|
|
|
| 89 |
elif llm_selection == "DialoGPT (GPT-2-1.5B)":
|
| 90 |
+
response = query_model(DIALOGPT_API_URL, {"inputs": chat_history})
|
| 91 |
+
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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
elif llm_selection == "Phi-3-mini-4k-instruct":
|
| 93 |
+
response = query_model(PHI3_API_URL, {"inputs": chat_history})
|
| 94 |
+
answer = response[0].get("generated_text", "No response") if isinstance(response, list) else "No response"
|
| 95 |
+
elif llm_selection == "Gemma-1.1-7B":
|
| 96 |
+
response = query_model(GEMMA_API_URL, {"inputs": chat_history})
|
| 97 |
+
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"
|
| 98 |
+
elif llm_selection == "Gemma-1.1-2B":
|
| 99 |
+
response = query_model(GEMMA_2B_API_URL, {"inputs": chat_history})
|
| 100 |
+
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"
|
| 101 |
+
elif llm_selection == "Meta-Llama-3-70B-Instruct":
|
| 102 |
+
response = query_model(META_LLAMA_70B_API_URL, {"inputs": chat_history})
|
| 103 |
+
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"
|
| 104 |
+
elif llm_selection == "Meta-Llama-3-8B-Instruct":
|
| 105 |
+
response = query_model(META_LLAMA_8B_API_URL, {"inputs": chat_history})
|
| 106 |
+
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"
|
| 107 |
elif llm_selection == "Gemma-2-27B":
|
| 108 |
+
response = query_model(GEMMA_27B_API_URL, {"inputs": chat_history})
|
| 109 |
+
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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
elif llm_selection == "Gemma-2-27B-IT":
|
| 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:
|
| 118 |
st.error(str(e))
|
| 119 |
|