File size: 3,701 Bytes
4bee38a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f4d75
4bee38a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import gradio as gr
from langchain_groq import ChatGroq
from langchain.schema import HumanMessage, SystemMessage

# Set up environment variables
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")

# Set up LLM client
llm = ChatGroq(temperature=0, model_name='mixtral-8x7b-32768', groq_api_key=GROQ_API_KEY)

# Chatbot function
def stream_chat(
    message: str,
    history: list,
    system_prompt: str,
    temperature: float = 0.5,
    max_tokens: int = 1024,
):
    conversation = [SystemMessage(content=system_prompt)]
    for prompt, answer in history:
        conversation.extend([
            HumanMessage(content=prompt),
            SystemMessage(content=answer),
        ])
    conversation.append(HumanMessage(content=message))

    # Update LLM parameters
    llm.temperature = temperature
    llm.max_tokens = max_tokens

    response = llm.stream(conversation)

    partial_message = ""
    for chunk in response:
        if chunk.content:
            partial_message += chunk.content
            yield partial_message

# Gradio Interface
CSS = """
.duplicate-button {
    margin: auto !important;
    color: white !important;
    background: black !important;
    border-radius: 100vh !important;
}
h3, p, h1 {
    text-align: center;
    color: white;
}
footer {
    text-align: center;
    padding: 10px;
    width: 100%;
    background-color: rgba(240, 240, 240, 0.8);
    z-index: 1000;
    position: relative;
    margin-top: 10px;
    color: black;
}
"""

PLACEHOLDER = """
<center>
<p>Hello! I'm here to assist you. Ask me anything :)</p>
</center>
"""

TITLE = "<h1><center>ConverseBot AI</center></h1>"

EXPLANATION = """
<div style="text-align: center; margin-top: 20px;">
    <p>This app uses the Mixtral-8x7B-32768 model, a powerful language model designed for high-quality, reasoning-rich interactions.</p>
</div>
"""

FOOTER_TEXT = """<footer> <p>If you enjoyed the functionality of the app, please leave a like!<br> Check out more on <a href="https://www.linkedin.com/in/girish-wangikar/" target="_blank">LinkedIn</a> | <a href="https://girishwangikar.github.io/Girish_Wangikar_Portfolio.github.io/" target="_blank">Portfolio</a></p></footer>"""

# Gradio app
with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
    gr.HTML(TITLE)
    gr.HTML(EXPLANATION)
    gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
    chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
    gr.ChatInterface(
        fn=stream_chat,
        chatbot=chatbot,
        fill_height=True,
        additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
        additional_inputs=[
            gr.Textbox(
                value="You are a helpful assistant",
                label="System Prompt",
                render=False,
            ),
            gr.Slider(
                minimum=0,
                maximum=1,
                step=0.1,
                value=0.5,
                label="Temperature",
                render=False,
            ),
            gr.Slider(
                minimum=128,
                maximum=8192,
                step=1,
                value=1024,
                label="Max tokens",
                render=False,
            ),
        ],
        examples=[
            ["What are some tips for mastering machine learning algorithms?"],
            ["What's a simple recipe for a healthy dinner?"],
            ["What are the benefits of meditation and how do I begin?"],
            ["Can you recommend some classic novels to read?"],
        ],
        cache_examples=False,
    )
    
    gr.HTML(FOOTER_TEXT)

# Launch the app
demo.launch(debug=True)