lucianommartins commited on
Commit
09565b8
·
1 Parent(s): b761217

My initial chatbot

Browse files
Files changed (2) hide show
  1. app.py +131 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import required modules
2
+ import os
3
+ import time
4
+ import base64
5
+ import gradio as gr
6
+ from PIL import Image
7
+ from google import genai
8
+ from google.genai import types
9
+
10
+ # define which Gemini model version is going to be used
11
+ model_id = "gemini-2.0-flash-exp"
12
+
13
+ # Gemini model system instruction settings
14
+ system_instruction="""
15
+ "You always reply in the same language the user sent the question. It is mandatory.",
16
+ "You only change the response language if explicitly asked - otherwise, answer in the original language."
17
+ "You are an assistant who helps people with their questions.",
18
+ "You only provide answers in one paragraph or less.",
19
+ "Your answers are long enough to not miss any information.",
20
+ "You are always kind and use simple, pleasant language.",
21
+ """
22
+
23
+
24
+ ## helper functions
25
+ # convert image files to base64 data
26
+ def image_to_base64(image_path):
27
+ with open(image_path, 'rb') as img:
28
+ encoded_string = base64.b64encode(img.read())
29
+ return encoded_string.decode('utf-8')
30
+
31
+
32
+ # show user message at the chatbot history
33
+ def query_message(history,txt,img):
34
+ if not img:
35
+ history += [(txt,None)]
36
+ return history
37
+ if img:
38
+ base64 = image_to_base64(img)
39
+ data_url = f"data:image/jpeg;base64,{base64}"
40
+ history += [(f"{txt} ![]({data_url})", None)]
41
+ return history
42
+
43
+ ## gradio interface
44
+ # gradio page variables
45
+ TITLE = """<h1 align="center">Gemini 2.0 Chatbot 🤖</h1>"""
46
+ SUBTITLE = """<h2 align="center">A multimodal chatbot powered by Gradio and Gemini API</h2>"""
47
+
48
+ # gradio styles in css
49
+ css = """
50
+ .container {
51
+ max-width: 100%;
52
+ padding: 0 1rem;
53
+ }
54
+
55
+ .chatbot {
56
+ height: calc(100vh - 250px) !important;
57
+ overflow-y: auto;
58
+ }
59
+
60
+ .textbox {
61
+ margin-top: 0.5rem;
62
+ }
63
+ """
64
+
65
+
66
+ # gradio chatbot main function
67
+ def registry(name, token, examples=None, **kwargs):
68
+ client = genai.Client(api_key=token)
69
+ chat = client.chats.create(
70
+ model=name,
71
+ config=types.GenerateContentConfig(
72
+ system_instruction=system_instruction,
73
+ temperature=0.5,
74
+ ),
75
+ )
76
+
77
+
78
+ # send user message to Gemini and append the answer at the chatbot history
79
+ def llm_response(history, text, img):
80
+ if not img:
81
+ response = chat.send_message(text)
82
+ history += [(None, response.text)]
83
+ return history, "", None
84
+ if img:
85
+ try:
86
+ img = Image.open(img)
87
+ response = chat.send_message([text, img])
88
+ history += [(None, response.text)]
89
+ return history, "", None
90
+ except Exception as e:
91
+ print("something wrong happened at the llm_response call")
92
+ print(str(e))
93
+ return history, "", None
94
+
95
+ # instantiate the gradio chatbot
96
+ print("Building the gradio app...")
97
+ with gr.Blocks(css=css) as app:
98
+ gr.HTML(TITLE)
99
+ gr.HTML(SUBTITLE)
100
+ with gr.Row():
101
+ # define the input fields - image input
102
+ image_box = gr.Image(type="filepath")
103
+ chatbot = gr.Chatbot(
104
+ scale=2,
105
+ height=500,
106
+ container=True
107
+ )
108
+
109
+ # define the input fields - text input
110
+ text_box = gr.Textbox(
111
+ placeholder="Type your message and press enter and optionally upload an image",
112
+ container=False,
113
+ )
114
+
115
+ # define the main chatbot logic
116
+ btn = gr.Button("Send")
117
+ clicked = btn.click(query_message,
118
+ [chatbot,text_box,image_box],
119
+ [chatbot]
120
+ ).then(llm_response,
121
+ [chatbot,text_box,image_box],
122
+ [chatbot, text_box, image_box]
123
+ )
124
+ return app
125
+
126
+ # launch the gradio chatbot
127
+ gr.load(
128
+ name=model_id,
129
+ src=registry,
130
+ accept_token=True
131
+ ).launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ google-genai
2
+ gradio==5.8.0