ziffir commited on
Commit
c82df34
1 Parent(s): 2a36277

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -163
app.py CHANGED
@@ -1,168 +1,111 @@
1
- import os
2
  import time
3
- from typing import List, Tuple, Optional
4
-
5
- import google.generativeai as genai
6
- import gradio as gr
 
 
7
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- print("google-generativeai:", genai.__version__)
10
-
11
- GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
12
-
13
- TITLE = """<h1 align="center" style="color: #4CAF50;">Gemini Playground 😎</h1>"""
14
- SUBTITLE = """<h2 align="center" style="color: #4CAF50;">Play with Gemini Pro and Gemini Pro Vision API 🖇️</h2>"""
15
- DUPLICATE = """
16
- <div style="text-align: center; display: flex; justify-content: center; align-items: center; color: #4CAF50;">
17
- <a href="https://huggingface.co/spaces/SkalskiP/ChatGemini?duplicate=true" style="text-decoration: none;">
18
- <img src="https://bit.ly/3gLdBN6" alt="Duplicate Space" style="margin-right: 10px;">
19
- <span style="font-weight: bold;">Duplicate the Space and run securely with your </span>
20
- <a href="https://makersuite.google.com/app/apikey" style="color: #2196F3; font-weight: bold;">GOOGLE API KEY</a>.
21
- </a>
22
- </div>
23
- """
24
-
25
-
26
- IMAGE_WIDTH = 512
27
-
28
-
29
- def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
30
- if not stop_sequences:
31
- return None
32
- return [sequence.strip() for sequence in stop_sequences.split(",")]
33
-
34
-
35
- def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
36
- image_height = int(image.height * IMAGE_WIDTH / image.width)
37
- return image.resize((IMAGE_WIDTH, image_height))
38
-
39
-
40
- def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
41
- return "", chatbot + [[text_prompt, None]]
42
-
43
-
44
- def bot(
45
- google_key: str,
46
- image_prompt: Optional[Image.Image],
47
- temperature: float,
48
- max_output_tokens: int,
49
- stop_sequences: str,
50
- top_k: int,
51
- top_p: float,
52
- chatbot: List[Tuple[str, str]]
53
- ):
54
- google_key = google_key if google_key else GOOGLE_API_KEY
55
- if not google_key:
56
- raise ValueError(
57
- "GOOGLE_API_KEY is not set. "
58
- "Please follow the instructions in the README to set it up.")
59
-
60
- text_prompt = chatbot[-1][0]
61
- genai.configure(api_key=google_key)
62
- generation_config = genai.types.GenerationConfig(
63
- temperature=temperature,
64
- max_output_tokens=max_output_tokens,
65
- stop_sequences=preprocess_stop_sequences(stop_sequences=stop_sequences),
66
- top_k=top_k,
67
- top_p=top_p
68
  )
69
 
70
- if image_prompt is None:
71
- model = genai.GenerativeModel('gemini-pro')
72
- response = model.generate_content(
73
- text_prompt,
74
- stream=True,
75
- generation_config=generation_config)
76
- response.resolve()
77
- else:
78
- image_prompt = preprocess_image(image_prompt)
79
- model = genai.GenerativeModel('gemini-pro-vision')
80
- response = model.generate_content(
81
- contents=[text_prompt, image_prompt],
82
- stream=True,
83
- generation_config=generation_config)
84
- response.resolve()
85
-
86
- # streaming effect
87
- chatbot[-1][1] = ""
88
- for chunk in response:
89
- for i in range(0, len(chunk.text), 10):
90
- section = chunk.text[i:i + 10]
91
- chatbot[-1][1] += section
92
- time.sleep(0.01)
93
- yield chatbot
94
-
95
-
96
- google_key_component = gr.Textbox(
97
- label="GOOGLE API KEY",
98
- value="",
99
- type="password",
100
- placeholder="...",
101
- info="You have to provide your own GOOGLE_API_KEY for this app to function properly",
102
- visible=GOOGLE_API_KEY is None
103
- )
104
-
105
- image_prompt_component = gr.Image(type="pil", label="Image", scale=1)
106
- chatbot_component = gr.Chatbot(
107
- label='Gemini',
108
- bubble_full_width=False,
109
- scale=2
110
- )
111
- text_prompt_component = gr.Textbox(
112
- placeholder="Hi there! Ask me anything and press Enter",
113
- label="",
114
- style="width: 90%;"
115
- )
116
- run_button_component = gr.Button(style="background-color: #4CAF50; color: white; border: none; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 5px;")
117
- temperature_component = gr.Slider(
118
- minimum=0,
119
- maximum=1.0,
120
- value=0.4,
121
- step=0.05,
122
- label="Temperature",
123
- info=(
124
- "Temperature controls the degree of randomness in token selection. Lower "
125
- "temperatures are good for prompts that expect a true or correct response, "
126
- "while higher temperatures can lead to more diverse or unexpected results."
127
- ))
128
- max_output_tokens_component = gr.Slider(
129
- minimum=1,
130
- maximum=2048,
131
- value=1024,
132
- step=1,
133
- label="Token limit",
134
- info=(
135
- "Token limit determines the maximum amount of text output from one prompt. A "
136
- "token is approximately four characters. The default value is 2048."
137
- ))
138
- stop_sequences_component = gr.Textbox(
139
- label="Add stop sequence",
140
- value="",
141
- type="text",
142
- placeholder="STOP, END",
143
- info=(
144
- "A stop sequence is a series of characters (including spaces) that stops "
145
- "response generation if the model encounters it. The sequence is not included "
146
- "as part of the response. You can add up to five stop sequences."
147
- ))
148
- top_k_component = gr.Slider(
149
- minimum=1,
150
- maximum=40,
151
- value=32,
152
- step=1,
153
- label="Top-K",
154
- info=(
155
- "Top-k changes how the model selects tokens for output. A top-k of 1 means the "
156
- "selected token is the most probable among all tokens in the model’s "
157
- "vocabulary (also called greedy decoding), while a top-k of 3 means that the "
158
- "next token is selected from among the 3 most probable tokens (using "
159
- "temperature)."
160
- ))
161
- top_p_component = gr.Slider(
162
- minimum=0,
163
- maximum=1,
164
- value=1,
165
- step=0.01,
166
- label="Top-P",
167
- info=(
168
- "Top
 
1
+ import streamlit as st
2
  import time
3
+ from openai import GPT3, GPT3APIError
4
+ from typing import List
5
+ import io
6
+ import numpy as np
7
+ import matplotlib.pyplot as plt
8
+ import pandas as pd
9
  from PIL import Image
10
+ import json
11
+ import random
12
+ import base64
13
+ import gr
14
+
15
+
16
+ GOOGLE_API_KEY = "your_google_api_key"
17
+ OPENAI_API_KEY = "your_openai_api_key"
18
+
19
+ st.set_option('deprecation.showfileUploaderEncoding', False)
20
+
21
+ openai_api = GPT3(engine="text-davinci-002", max_tokens=1024, temperature=0.4, top_p=1, frequency_penalty=0.0, presence_penalty=0.0)
22
+
23
+ st.title("Gemini")
24
+
25
+ def main():
26
+ if GOOGLE_API_KEY is None:
27
+ st.warning("Please provide your GOOGLE_API_KEY")
28
+ return
29
+
30
+ # Render components
31
+ global GOOGLE_API_KEY, OPENAI_API_KEY
32
+ global image_prompt_component, chatbot_component, text_prompt_component, run_button_component
33
+ global temperature_component, max_output_tokens_component, stop_sequences_component, top_k_component, top_p_component
34
+
35
+ google_key_component = st.text_input(
36
+ label="GOOGLE API KEY",
37
+ value="",
38
+ type="password",
39
+ placeholder="...",
40
+ help="You have to provide your own GOOGLE_API_KEY for this app to function properly",
41
+ visible=GOOGLE_API_KEY is None
42
+ )
43
+
44
+ image_prompt_component = st.image_uploader(
45
+ label="Image",
46
+ type="pil",
47
+ accept_jpg=True,
48
+ accept_png=True,
49
+ help="Upload an image or drag-and-drop one onto the canvas."
50
+ )
51
+
52
+ chatbot_component = st.empty()
53
+
54
+ text_prompt_component = st.text_input(
55
+ label="",
56
+ value="",
57
+ max_chars=500,
58
+ help="Type your message here and press Enter."
59
+ )
60
+
61
+ run_button_component = st.button(
62
+ label="Run",
63
+ help="Generate a response to your input."
64
+ )
65
+
66
+ temperature_component = st.slider(
67
+ label="Temperature",
68
+ min_value=0.0,
69
+ max_value=1.0,
70
+ value=0.4,
71
+ step=0.05,
72
+ help=(
73
+ "Temperature controls the degree of randomness in token selection. Lower "
74
+ "temperatures are good for prompts that expect a true or correct response, "
75
+ "while higher temperatures can lead to more diverse or unexpected results."
76
+ )
77
+ )
78
+
79
+ max_output_tokens_component = st.slider(
80
+ label="Token limit",
81
+ min_value=1,
82
+ max_value=2048,
83
+ value=1024,
84
+ step=1,
85
+ help=(
86
+ "Token limit determines the maximum amount of text output from one prompt. A "
87
+ "token is approximately four characters. The default value is 2048."
88
+ )
89
+ )
90
 
91
+ stop_sequences_component = st.text_input(
92
+ label="Add stop sequence",
93
+ value="",
94
+ type="text",
95
+ placeholder="STOP, END",
96
+ help=(
97
+ "A stop sequence is a series of characters (including spaces) that stops "
98
+ "response generation if the model encounters it. The sequence is not included "
99
+ "as part of the response. You can add up to five stop sequences."
100
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  )
102
 
103
+ top_k_component = st.slider(
104
+ label="Top-K",
105
+ min_value=1,
106
+ max_value=40,
107
+ value=32,
108
+ step=1,
109
+ help=(
110
+ "Top-k changes how the model selects tokens for output. A top-k of 1 means the "
111
+ "selected token is the most probable among all tokens in the