File size: 6,200 Bytes
9fdb387
 
 
 
 
 
 
61e7204
13d3b4a
9fdb387
f02a829
61e7204
f02a829
61e7204
9fdb387
 
61e7204
13d3b4a
61e7204
 
ac85bcd
61e7204
 
f02a829
 
ac85bcd
9fdb387
13d3b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61e7204
a05cc1e
 
 
0efcae6
 
 
9fdb387
 
 
 
a05cc1e
 
9fdb387
 
13d3b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cde5d2
13d3b4a
 
 
 
 
9fdb387
 
 
a05cc1e
 
9fdb387
a05cc1e
 
c75a093
9fdb387
 
 
 
61e7204
13d3b4a
9fdb387
 
 
 
 
 
 
 
 
 
a05cc1e
61e7204
a05cc1e
 
 
0efcae6
 
 
 
61e7204
 
 
a05cc1e
0efcae6
13d3b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fdb387
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import os
import google.generativeai as genai
import gradio as gr
from dotenv import load_dotenv

load_dotenv()

GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
# model_name = "gemini-1.5-flash-exp-0827"

TITLE = """<h1 align="center">๐ŸŽฎChat with Gemini 1.5๐Ÿ”ฅ</h1>"""
NOTICE = """
**Notices** ๐Ÿ“œ:
- This app is still in development
- Some features may not work as expected
"""
ABOUT = """
**Updates (2024-9-25)** ๐Ÿ“ˆ: Upgrade model to SOTA Gemini 1.5 Flash Experimental 0924
"""
ERRORS = """
Known errors โš ๏ธ:
"""
FUTURE_IMPLEMENTATIONS = """
Future features ๐Ÿš€:
- More tools such as web search
"""

# genai.configure(api_key=GEMINI_API_KEY)
# model = genai.GenerativeModel(
#     model_name,
#     safety_settings=[
#         {
#             "category": "HARM_CATEGORY_HARASSMENT",
#             "threshold": "BLOCK_NONE"
#         },
#         {
#             "category": "HARM_CATEGORY_HATE_SPEECH",
#             "threshold": "BLOCK_NONE"
#         },
#         {
#             "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
#             "threshold": "BLOCK_NONE"
#         },
#         {
#             "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
#             "threshold": "BLOCK_NONE"
#         }
#     ],
#     generation_config={
#         "temperature": 1,
#         "top_p": 0.95,
#         "top_k": 64,
#         "max_output_tokens": 8192,
#         "response_mime_type": "text/plain",
#     }
# )
# chat = model.start_chat(history=[])

# def clear_chat_history():
#     chat.history = []
    
# def undo_chat():
#     last_send, last_received = chat.rewind()

# def transform_history(history):
#     new_history = []
#     for user_msg, model_msg in history:
#         new_history.append({"role": "user", "parts": [user_msg]})
#         new_history.append({"role": "model", "parts": [model_msg]})
#     return new_history

# def chatbot_stable(message, history):
#     message_text = message["text"]
#     message_files = message["files"]
#     if message_files:
#         image_uris = [genai.upload_file(path=file_path["path"]) for file_path in message_files]
#         message_content = [message_text] + image_uris
#     else:
#         message_content = [message_text]

#     response = chat.send_message(message_content, stream=True)
#     response.resolve()

#     return response.text

# gemini_chatbot_interface = gr.Chatbot(
#     height=400,
#     likeable=True,
#     avatar_images=(
#         None,
#         "https://media.roboflow.com/spaces/gemini-icon.png"
#     ),
#     show_copy_button=True,
#     show_share_button=True,
#     render_markdown=True
# )

# clear_chat_button = gr.ClearButton(
#     components=[gemini_chatbot_interface],
#     value="๐Ÿ—‘๏ธ Clear"
# )

# undo_chat_button = gr.Button(
#     value="โ†ฉ๏ธ Undo"
# )

# gemini_chatbot = gr.ChatInterface(
#     fn=chatbot_stable,
#     chatbot=gemini_chatbot_interface,
#     multimodal=True,
#     clear_btn=clear_chat_button,
#     undo_btn=undo_chat_button
# )

model_list = ["gemini-1.5-pro", "gemini-1.5-pro-002", "gemini-1.5-pro-exp-0827", "gemini-1.5-flash", "gemini-1.5-flash-002", "gemini-1.5-flash-8b-exp-0924"]

genai.configure(api_key=GEMINI_API_KEY)

def clear_chat_history():
    chat.history = []
    
def undo_chat():
    last_send, last_received = chat.rewind()

def transform_history(history):
    new_history = []
    for user_msg, model_msg in history:
        new_history.append({"role": "user", "parts": [user_msg]})
        new_history.append({"role": "model", "parts": [model_msg]})
    return new_history

def chatbot_stable(message, history, model_id, system_message, max_tokens, temperature, top_p,):
    global model, chat
    model = genai.GenerativeModel(
        model_name=model_id,
        system_instruction=system_message,
        safety_settings=[
            {
                "category": "HARM_CATEGORY_HARASSMENT",
                "threshold": "BLOCK_NONE"
            },
            {
                "category": "HARM_CATEGORY_HATE_SPEECH",
                "threshold": "BLOCK_NONE"
            },
            {
                "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                "threshold": "BLOCK_NONE"
            },
            {
                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                "threshold": "BLOCK_NONE"
            }
        ],
        generation_config={
            "temperature": temperature,
            "top_p": top_p,
            "top_k": 40,
            "max_output_tokens": max_tokens,
            "response_mime_type": "text/plain",
        }
    )
    chat = model.start_chat(history=[])
    message_text = message["text"]
    message_files = message["files"]
    if message_files:
        image_uris = [genai.upload_file(path=file_path["path"]) for file_path in message_files]
        message_content = [message_text] + image_uris
    else:
        message_content = [message_text]

    response = chat.send_message(message_content, stream=True)
    response.resolve()

    return response.text

gemini_chatbot_interface = gr.Chatbot(
    height=500,
    likeable=True,
    avatar_images=(
        None,
        "https://media.roboflow.com/spaces/gemini-icon.png"
    ),
    show_copy_button=True,
    show_share_button=True,
    render_markdown=True
)

clear_chat_button = gr.ClearButton(
    components=[gemini_chatbot_interface],
    value="๐Ÿ—‘๏ธ Clear"
)

undo_chat_button = gr.Button(
    value="โ†ฉ๏ธ Undo"
)

gemini_chatbot = gr.ChatInterface(
    fn=chatbot_stable,
    chatbot=gemini_chatbot_interface,
    multimodal=True,
    clear_btn=clear_chat_button,
    undo_btn=undo_chat_button,
    additional_inputs=[
        gr.Dropdown(
            choices=model_list,
            value="gemini-1.5-flash-002",
            label="Models"
        ),
        gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
        gr.Slider(minimum=1, maximum=8192, value=4096, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=1.0, value=1, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
    ],
)