File size: 3,775 Bytes
47fd10e
 
 
 
 
 
 
 
 
 
 
 
eea8fc0
47fd10e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eea8fc0
47fd10e
 
eea8fc0
47fd10e
 
 
 
 
 
 
 
 
 
 
 
 
eea8fc0
 
47fd10e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import requests
import streamlit as st

# Get the Hugging Face API Token from environment variables
HF_API_TOKEN = os.getenv("HF_API_KEY")
if not HF_API_TOKEN:
    raise ValueError("Hugging Face API Token is not set in the environment variables.")

# Hugging Face API URLs and headers for Gemma models
GEMMA_7B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-1.1-7b-it"
GEMMA_27B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-2-27b-it"
CODEGEMMA_7B_API_URL = "https://api-inference.huggingface.co/models/google/codegemma-7b"

HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}

def query_model(api_url, payload):
    response = 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))

# Streamlit app
st.set_page_config(page_title="Gemma Chatbot Interface", layout="wide")
st.title("Gemma Chatbot Interface")
st.write("Gemma Chatbot Interface")

# Initialize session state for conversation and model history
if "conversation" not in st.session_state:
    st.session_state.conversation = []
if "model_history" not in st.session_state:
    st.session_state.model_history = {model: [] for model in ["Gemma-1.1-7B", "Gemma-2-27B", "codegemma-7b"]}

# Dropdown for Gemma model selection
gemma_selection = st.selectbox("Select Gemma Model", ["Gemma-1.1-7B", "Gemma-2-27B", "codegemma-7b"])

# User input for question
question = st.text_input("Question", placeholder="Enter your question here...")

# Handle user input and Gemma model response
if st.button("Send") and question:
    try:
        with st.spinner("Waiting for the model to respond..."):
            chat_history = " ".join(st.session_state.model_history[gemma_selection]) + f"User: {question}\n"
            if gemma_selection == "Gemma-1.1-7B":
                response = query_model(GEMMA_7B_API_URL, {"inputs": chat_history})
            elif gemma_selection == "Gemma-2-27B":
                response = query_model(GEMMA_27B_API_URL, {"inputs": chat_history})
            elif gemma_selection == "codegemma-7b":
                response = query_model(CODEGEMMA_7B_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, gemma_selection)
            st.session_state.model_history[gemma_selection].append(f"User: {question}\n{gemma_selection}: {answer}\n")
    except ValueError as e:
        st.error(str(e))

# Custom CSS for chat bubbles
st.markdown(
    """
    <style>
    .chat-bubble {
        padding: 10px 14px;
        border-radius: 14px;
        margin-bottom: 10px;
        display: inline-block;
        max-width: 80%;
        color: black;
    }
    .chat-bubble.user {
        background-color: #dcf8c6;
        align-self: flex-end;
    }
    .chat-bubble.bot {
        background-color: #fff;
        align-self: flex-start;
    }
    .chat-container {
        display: flex;
        flex-direction: column;
        gap: 10px;
        margin-top: 20px;
    }
    </style>
    """,
    unsafe_allow_html=True
)

# Display the conversation
st.write('<div class="chat-container">', unsafe_allow_html=True)
for user_message, bot_message, model_name in st.session_state.conversation:
    st.write(f'<div class="chat-bubble user">You: {user_message}</div>', unsafe_allow_html=True)
    st.write(f'<div class="chat-bubble bot">{model_name}: {bot_message}</div>', unsafe_allow_html=True)
st.write('</div>', unsafe_allow_html=True)