File size: 1,915 Bytes
b0397d2
 
 
 
 
a602ed1
b0397d2
89b4d20
da9fe57
1c76710
de24765
08cc93e
 
 
de24765
6f9707c
 
 
16f0ae2
6f9707c
e1037bf
08cc93e
de24765
6f9707c
 
 
de24765
4e4c8dd
b0397d2
39690f6
 
 
de24765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39690f6
de24765
 
7e0e867
08cc93e
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
import os
import gradio as gr
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI

# Set the path to the service account key
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./firm-catalyst-437006-s4-407500537db5.json"

# Initialize the language model with required parameters
llm = ChatGoogleGenerativeAI(model='gemini-1.5-pro')  

def chat_with_gemini(message, chat_history):
    # Generate a response using the language model
    bot_response = llm.predict(message)  # Get the bot's response from the model
    chat_history.append((message, bot_response))  # Append the user and bot messages as a tuple
    return chat_history, chat_history

# Create a Gradio interface
with gr.Blocks() as iface:
    gr.Markdown("# Ken Chatbot")
    gr.Markdown("Ask me anything!")

    chatbot = gr.Chatbot(elem_id="chatbot")  # Initialize the chatbot with an element ID for styling
    msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message and press enter to send your message...")
    state = gr.State([])  # Store chat history

    # Set up the interaction for when the user submits a message
    msg.submit(chat_with_gemini, [msg, state], [chatbot, state])  # Update chatbot and state with new messages
    msg.submit(lambda: "", None, msg)  # Clear the input box after submission

    # Custom CSS for styling the chatbot messages
    gr.HTML("""
    <style>
    #chatbot .message.user {
        background-color: #DCF8C6;
        border-radius: 15px;
        padding: 8px 12px;
        margin: 5px 50px 5px auto;
        max-width: 70%;
        text-align: right;
    }
    #chatbot .message.bot {
        background-color: #E1E1E1;
        border-radius: 15px;
        padding: 8px 12px;
        margin: 5px auto 5px 50px;
        max-width: 70%;
        text-align: left;
    }
    </style>
    """)

# Launch the interface with debugging enabled
iface.launch(debug=True)