Spaces:
Runtime error
Runtime error
Raghavan1988
commited on
Commit
•
4761bf9
1
Parent(s):
c3ae50d
Adding OPEN AI calls
Browse files- app.py +26 -4
- requirements.txt +1 -0
app.py
CHANGED
@@ -5,10 +5,12 @@ from typing import List, Tuple, Optional, Dict, Union
|
|
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">Gemini PolyGLOT that can understand languages beyond english</h1>"""
|
14 |
SUBTITLE = """"""
|
@@ -32,6 +34,21 @@ IMAGE_CACHE_DIRECTORY = "/tmp"
|
|
32 |
IMAGE_WIDTH = 512
|
33 |
CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
37 |
if not stop_sequences:
|
@@ -89,7 +106,8 @@ def bot(
|
|
89 |
stop_sequences: str,
|
90 |
top_k: int,
|
91 |
top_p: float,
|
92 |
-
chatbot: CHAT_HISTORY
|
|
|
93 |
):
|
94 |
if len(chatbot) == 0:
|
95 |
return chatbot
|
@@ -122,8 +140,11 @@ def bot(
|
|
122 |
text_prompt = [chatbot[-1][0]] \
|
123 |
if chatbot[-1][0] and isinstance(chatbot[-1][0], str) \
|
124 |
else []
|
125 |
-
|
|
|
126 |
model = genai.GenerativeModel('gemini-pro')
|
|
|
|
|
127 |
response = model.generate_content(
|
128 |
text_prompt,
|
129 |
stream=True,
|
@@ -223,6 +244,7 @@ user_inputs = [
|
|
223 |
text_prompt_component,
|
224 |
chatbot_component
|
225 |
]
|
|
|
226 |
|
227 |
bot_inputs = [
|
228 |
google_key_component,
|
@@ -232,13 +254,13 @@ bot_inputs = [
|
|
232 |
stop_sequences_component,
|
233 |
top_k_component,
|
234 |
top_p_component,
|
235 |
-
chatbot_component
|
|
|
236 |
]
|
237 |
|
238 |
with gr.Blocks() as demo:
|
239 |
gr.HTML(TITLE)
|
240 |
gr.HTML(SUBTITLE)
|
241 |
-
language = gr.Textbox(label="description", value = "en")
|
242 |
with gr.Column():
|
243 |
google_key_component.render()
|
244 |
chatbot_component.render()
|
|
|
5 |
import google.generativeai as genai
|
6 |
import gradio as gr
|
7 |
from PIL import Image
|
8 |
+
from openai import OpenAI
|
9 |
|
10 |
print("google-generativeai:", genai.__version__)
|
11 |
|
12 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
13 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
14 |
|
15 |
TITLE = """<h1 align="center">Gemini PolyGLOT that can understand languages beyond english</h1>"""
|
16 |
SUBTITLE = """"""
|
|
|
34 |
IMAGE_WIDTH = 512
|
35 |
CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
|
36 |
|
37 |
+
def translate_to_english(text):
|
38 |
+
response = client.chat.completions.create(model = "gpt-3.5-turbo",
|
39 |
+
messages = [
|
40 |
+
{"role": "system", "content": "You are a translator to English. Check if the input is in English. If the input is english DO NOTHING and return AS-IS else Translate to English"},
|
41 |
+
{"role": "user", "content": text},
|
42 |
+
],stream = False, )
|
43 |
+
return response.choices[0].message.content
|
44 |
+
|
45 |
+
def translate_from_english(text, language):
|
46 |
+
response = client.chat.completions.create(model = "gpt-3.5-turbo",
|
47 |
+
messages = [
|
48 |
+
{"role": "system", "content": "You are a translator to " + language + " Translate english to " + language},
|
49 |
+
{"role": "user", "content": text},
|
50 |
+
],stream = False, )
|
51 |
+
return response.choices[0].message.content
|
52 |
|
53 |
def preprocess_stop_sequences(stop_sequences: str) -> Optional[List[str]]:
|
54 |
if not stop_sequences:
|
|
|
106 |
stop_sequences: str,
|
107 |
top_k: int,
|
108 |
top_p: float,
|
109 |
+
chatbot: CHAT_HISTORY,
|
110 |
+
language: str
|
111 |
):
|
112 |
if len(chatbot) == 0:
|
113 |
return chatbot
|
|
|
140 |
text_prompt = [chatbot[-1][0]] \
|
141 |
if chatbot[-1][0] and isinstance(chatbot[-1][0], str) \
|
142 |
else []
|
143 |
+
if (language.startswith("en") == False):
|
144 |
+
text_prompt = [translate_to_english(text_prompt[0])]
|
145 |
model = genai.GenerativeModel('gemini-pro')
|
146 |
+
|
147 |
+
|
148 |
response = model.generate_content(
|
149 |
text_prompt,
|
150 |
stream=True,
|
|
|
244 |
text_prompt_component,
|
245 |
chatbot_component
|
246 |
]
|
247 |
+
language = gr.Textbox(label="description", value = "en")
|
248 |
|
249 |
bot_inputs = [
|
250 |
google_key_component,
|
|
|
254 |
stop_sequences_component,
|
255 |
top_k_component,
|
256 |
top_p_component,
|
257 |
+
chatbot_component,
|
258 |
+
language
|
259 |
]
|
260 |
|
261 |
with gr.Blocks() as demo:
|
262 |
gr.HTML(TITLE)
|
263 |
gr.HTML(SUBTITLE)
|
|
|
264 |
with gr.Column():
|
265 |
google_key_component.render()
|
266 |
chatbot_component.render()
|
requirements.txt
CHANGED
@@ -2,3 +2,4 @@ google-generativeai==0.3.1
|
|
2 |
gradio
|
3 |
googletrans==3.1.0a0
|
4 |
trulens_eval==0.19.2
|
|
|
|
2 |
gradio
|
3 |
googletrans==3.1.0a0
|
4 |
trulens_eval==0.19.2
|
5 |
+
openai===1.6.1
|