Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Commit
•
c586aa5
1
Parent(s):
eea8fc0
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,8 @@ HF_API_TOKEN = os.getenv("HF_API_KEY")
|
|
7 |
if not HF_API_TOKEN:
|
8 |
raise ValueError("Hugging Face API Token is not set in the environment variables.")
|
9 |
|
10 |
-
# Hugging Face API
|
11 |
-
GEMMA_7B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-1.1-7b-it"
|
12 |
GEMMA_27B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-2-27b-it"
|
13 |
-
CODEGEMMA_7B_API_URL = "https://api-inference.huggingface.co/models/google/codegemma-7b"
|
14 |
|
15 |
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
16 |
|
@@ -21,38 +19,35 @@ def query_model(api_url, payload):
|
|
21 |
def add_message_to_conversation(user_message, bot_message, model_name):
|
22 |
st.session_state.conversation.append((user_message, bot_message, model_name))
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
st.
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
st.session_state.model_history = {model: [] for model in ["Gemma-1.1-7B", "Gemma-2-27B", "codegemma-7b"]}
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
|
38 |
# User input for question
|
39 |
question = st.text_input("Question", placeholder="Enter your question here...")
|
40 |
|
41 |
-
# Handle user input and Gemma model response
|
42 |
if st.button("Send") and question:
|
43 |
try:
|
44 |
with st.spinner("Waiting for the model to respond..."):
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
response = query_model(CODEGEMMA_7B_API_URL, {"inputs": chat_history})
|
52 |
-
|
53 |
-
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"
|
54 |
-
add_message_to_conversation(question, answer, gemma_selection)
|
55 |
-
st.session_state.model_history[gemma_selection].append(f"User: {question}\n{gemma_selection}: {answer}\n")
|
56 |
except ValueError as e:
|
57 |
st.error(str(e))
|
58 |
|
|
|
7 |
if not HF_API_TOKEN:
|
8 |
raise ValueError("Hugging Face API Token is not set in the environment variables.")
|
9 |
|
10 |
+
# Hugging Face API URL and header for Gemma 27B-it model
|
|
|
11 |
GEMMA_27B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-2-27b-it"
|
|
|
12 |
|
13 |
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
14 |
|
|
|
19 |
def add_message_to_conversation(user_message, bot_message, model_name):
|
20 |
st.session_state.conversation.append((user_message, bot_message, model_name))
|
21 |
|
22 |
+
def initialize_memory():
|
23 |
+
if "memory" not in st.session_state:
|
24 |
+
st.session_state.memory = {"user_inputs": [], "bot_responses": []}
|
25 |
+
|
26 |
+
def update_memory(user_message, bot_message):
|
27 |
+
st.session_state.memory["user_inputs"].append(user_message)
|
28 |
+
st.session_state.memory["bot_responses"].append(bot_message)
|
29 |
|
30 |
+
# Streamlit app
|
31 |
+
st.set_page_config(page_title="Gemma 27B-it Chatbot Interface", layout="wide")
|
32 |
+
st.title("Gemma 27B-it Chatbot Interface")
|
33 |
+
st.write("Gemma 27B-it Chatbot Interface")
|
|
|
34 |
|
35 |
+
# Initialize session state for conversation and memory
|
36 |
+
initialize_memory()
|
37 |
|
38 |
# User input for question
|
39 |
question = st.text_input("Question", placeholder="Enter your question here...")
|
40 |
|
41 |
+
# Handle user input and Gemma 27B-it model response
|
42 |
if st.button("Send") and question:
|
43 |
try:
|
44 |
with st.spinner("Waiting for the model to respond..."):
|
45 |
+
# Construct the chat history including memory
|
46 |
+
chat_history = " ".join(st.session_state.memory["bot_responses"][-5:]) + f"User: {question}\n"
|
47 |
+
response = query_model(GEMMA_27B_API_URL, {"inputs": chat_history})
|
48 |
+
answer = response.get("generated_text", "No response")
|
49 |
+
add_message_to_conversation(question, answer, "Gemma-2-27B-it")
|
50 |
+
update_memory(question, answer)
|
|
|
|
|
|
|
|
|
|
|
51 |
except ValueError as e:
|
52 |
st.error(str(e))
|
53 |
|