SkalskiP commited on
Commit
eeb8511
1 Parent(s): 908d449

`app.py` updates

Browse files
Files changed (1) hide show
  1. app.py +45 -11
app.py CHANGED
@@ -1,19 +1,42 @@
1
- from typing import List, Tuple
2
 
3
- import gradio as gr
4
  import google.generativeai as genai
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
 
7
- def predict(google_key: str, text_prompt: str, chatbot: List[Tuple[str, str]]):
 
 
 
 
 
8
  if not google_key:
9
  raise ValueError(
10
  "GOOGLE_API_KEY is not set. "
11
  "Please follow the instructions in the README to set it up.")
12
 
13
  genai.configure(api_key=google_key)
14
- model = genai.GenerativeModel('models/gemini-pro')
15
- response = model.generate_content(text_prompt, stream=True)
16
- response.resolve()
 
 
 
 
 
 
 
17
  chatbot.append((text_prompt, response.text))
18
  return "", chatbot
19
 
@@ -23,33 +46,44 @@ google_key_component = gr.Textbox(
23
  value="",
24
  type="password",
25
  placeholder="...",
26
- info="You have to provide your own GPT4 keys for this app to function properly",
27
  )
28
 
29
- chatbot_component = gr.Chatbot(label='Gemini')
 
30
  text_prompt_component = gr.Textbox(
31
  placeholder="Hi there!",
32
- label="Type an input and press Enter"
33
  )
34
  run_button_component = gr.Button()
35
 
 
 
 
 
 
 
 
36
  with gr.Blocks() as demo:
 
 
37
  with gr.Column():
38
  google_key_component.render()
39
  with gr.Row():
 
40
  chatbot_component.render()
41
  text_prompt_component.render()
42
  run_button_component.render()
43
 
44
  run_button_component.click(
45
  fn=predict,
46
- inputs=[google_key_component, text_prompt_component, chatbot_component],
47
  outputs=[text_prompt_component, chatbot_component],
48
  )
49
 
50
  text_prompt_component.submit(
51
  fn=predict,
52
- inputs=[google_key_component, text_prompt_component, chatbot_component],
53
  outputs=[text_prompt_component, chatbot_component],
54
  )
55
 
 
1
+ from typing import List, Tuple, Optional
2
 
 
3
  import google.generativeai as genai
4
+ import gradio as gr
5
+ from PIL import Image
6
+
7
+ TITLE = """<h1 align="center">Gemini Pro and Pro Vision via API 🚀</h1>"""
8
+ DUPLICATE = """
9
+ <div style="text-align: center; display: flex; justify-content: center; align-items: center;">
10
+ <a href="https://huggingface.co/spaces/SkalskiP/ChatGemini?duplicate=true">
11
+ <img src="https://bit.ly/3gLdBN6" alt="Duplicate Space" style="margin-right: 10px;">
12
+ </a>
13
+ <span>Duplicate the Space and run securely with your GOOGLE API KEY.</span>
14
+ </div>
15
+ """
16
 
17
 
18
+ def predict(
19
+ google_key: str,
20
+ text_prompt: str,
21
+ image_prompt: Optional[Image.Image],
22
+ chatbot: List[Tuple[str, str]]
23
+ ) -> Tuple[str, List[Tuple[str, str]]]:
24
  if not google_key:
25
  raise ValueError(
26
  "GOOGLE_API_KEY is not set. "
27
  "Please follow the instructions in the README to set it up.")
28
 
29
  genai.configure(api_key=google_key)
30
+
31
+ if image_prompt is None:
32
+ model = genai.GenerativeModel('gemini-pro')
33
+ response = model.generate_content(text_prompt, stream=True)
34
+ response.resolve()
35
+ else:
36
+ model = genai.GenerativeModel('gemini-pro-vision')
37
+ response = model.generate_content([text_prompt, image_prompt], stream=True)
38
+ response.resolve()
39
+
40
  chatbot.append((text_prompt, response.text))
41
  return "", chatbot
42
 
 
46
  value="",
47
  type="password",
48
  placeholder="...",
49
+ info="You have to provide your own GOOGLE_API_KEY for this app to function properly",
50
  )
51
 
52
+ image_prompt_component = gr.Image(type="pil", label="Image", scale=1)
53
+ chatbot_component = gr.Chatbot(label='Gemini', scale=2)
54
  text_prompt_component = gr.Textbox(
55
  placeholder="Hi there!",
56
+ label="Ask me anything and press Enter"
57
  )
58
  run_button_component = gr.Button()
59
 
60
+ inputs = [
61
+ google_key_component,
62
+ text_prompt_component,
63
+ image_prompt_component,
64
+ chatbot_component
65
+ ]
66
+
67
  with gr.Blocks() as demo:
68
+ gr.HTML(TITLE)
69
+ gr.HTML(DUPLICATE)
70
  with gr.Column():
71
  google_key_component.render()
72
  with gr.Row():
73
+ image_prompt_component.render()
74
  chatbot_component.render()
75
  text_prompt_component.render()
76
  run_button_component.render()
77
 
78
  run_button_component.click(
79
  fn=predict,
80
+ inputs=inputs,
81
  outputs=[text_prompt_component, chatbot_component],
82
  )
83
 
84
  text_prompt_component.submit(
85
  fn=predict,
86
+ inputs=inputs,
87
  outputs=[text_prompt_component, chatbot_component],
88
  )
89