girishwangikar commited on
Commit
4bee38a
1 Parent(s): 3743601

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from langchain_groq import ChatGroq
4
+ from langchain.schema import HumanMessage, SystemMessage
5
+
6
+ # Set up environment variables
7
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
8
+
9
+ # Set up LLM client
10
+ llm = ChatGroq(temperature=0, model_name='mixtral-8x7b-32768', groq_api_key=GROQ_API_KEY)
11
+
12
+ # Chatbot function
13
+ def stream_chat(
14
+ message: str,
15
+ history: list,
16
+ system_prompt: str,
17
+ temperature: float = 0.5,
18
+ max_tokens: int = 1024,
19
+ ):
20
+ conversation = [SystemMessage(content=system_prompt)]
21
+ for prompt, answer in history:
22
+ conversation.extend([
23
+ HumanMessage(content=prompt),
24
+ SystemMessage(content=answer),
25
+ ])
26
+ conversation.append(HumanMessage(content=message))
27
+
28
+ # Update LLM parameters
29
+ llm.temperature = temperature
30
+ llm.max_tokens = max_tokens
31
+
32
+ response = llm.stream(conversation)
33
+
34
+ partial_message = ""
35
+ for chunk in response:
36
+ if chunk.content:
37
+ partial_message += chunk.content
38
+ yield partial_message
39
+
40
+ # Gradio Interface
41
+ CSS = """
42
+ .duplicate-button {
43
+ margin: auto !important;
44
+ color: white !important;
45
+ background: black !important;
46
+ border-radius: 100vh !important;
47
+ }
48
+ h3, p, h1 {
49
+ text-align: center;
50
+ color: white;
51
+ }
52
+ footer {
53
+ text-align: center;
54
+ padding: 10px;
55
+ width: 100%;
56
+ background-color: rgba(240, 240, 240, 0.8);
57
+ z-index: 1000;
58
+ position: relative;
59
+ margin-top: 10px;
60
+ color: black;
61
+ }
62
+ """
63
+
64
+ PLACEHOLDER = """
65
+ <center>
66
+ <p>Hello! I'm here to assist you. Ask me anything :)</p>
67
+ </center>
68
+ """
69
+
70
+ TITLE = "<h1><center>Chatbot-Mixtral</center></h1>"
71
+
72
+ EXPLANATION = """
73
+ <div style="text-align: center; margin-top: 20px;">
74
+ <p>This app uses the Mixtral-8x7B-32768 model, a powerful language model designed for high-quality, reasoning-rich interactions.</p>
75
+ </div>
76
+ """
77
+
78
+ 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>"""
79
+
80
+ # Gradio app
81
+ with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
82
+ gr.HTML(TITLE)
83
+ gr.HTML(EXPLANATION)
84
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
85
+ chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
86
+ gr.ChatInterface(
87
+ fn=stream_chat,
88
+ chatbot=chatbot,
89
+ fill_height=True,
90
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
91
+ additional_inputs=[
92
+ gr.Textbox(
93
+ value="You are a helpful assistant",
94
+ label="System Prompt",
95
+ render=False,
96
+ ),
97
+ gr.Slider(
98
+ minimum=0,
99
+ maximum=1,
100
+ step=0.1,
101
+ value=0.5,
102
+ label="Temperature",
103
+ render=False,
104
+ ),
105
+ gr.Slider(
106
+ minimum=128,
107
+ maximum=8192,
108
+ step=1,
109
+ value=1024,
110
+ label="Max tokens",
111
+ render=False,
112
+ ),
113
+ ],
114
+ examples=[
115
+ ["What are some tips for mastering machine learning algorithms?"],
116
+ ["What's a simple recipe for a healthy dinner?"],
117
+ ["What are the benefits of meditation and how do I begin?"],
118
+ ["Can you recommend some classic novels to read?"],
119
+ ],
120
+ cache_examples=False,
121
+ )
122
+
123
+ gr.HTML(FOOTER_TEXT)
124
+
125
+ # Launch the app
126
+ demo.launch(debug=True)