Spaces:
Build error
Build error
Delete maya2
Browse files
maya2
DELETED
|
@@ -1,248 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
from google.cloud import storage
|
| 11 |
-
from google.oauth2 import service_account
|
| 12 |
-
import json
|
| 13 |
-
import os
|
| 14 |
-
import requests
|
| 15 |
-
|
| 16 |
-
# upload image to google cloud storage
|
| 17 |
-
def upload_file_to_gcs_blob(file):
|
| 18 |
-
|
| 19 |
-
google_creds = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
| 20 |
-
|
| 21 |
-
creds_json = json.loads(google_creds)
|
| 22 |
-
credentials = service_account.Credentials.from_service_account_info(creds_json)
|
| 23 |
-
|
| 24 |
-
# Google Cloud credentials
|
| 25 |
-
storage_client = storage.Client(credentials=credentials, project=creds_json['project_id'])
|
| 26 |
-
|
| 27 |
-
bucket_name=os.environ.get('bucket_name')
|
| 28 |
-
bucket = storage_client.bucket(bucket_name)
|
| 29 |
-
|
| 30 |
-
destination_blob_name = os.path.basename(file)
|
| 31 |
-
blob = bucket.blob(destination_blob_name)
|
| 32 |
-
|
| 33 |
-
blob.upload_from_filename(file)
|
| 34 |
-
|
| 35 |
-
public_url = blob.public_url
|
| 36 |
-
|
| 37 |
-
return public_url
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
from PIL import Image
|
| 41 |
-
|
| 42 |
-
def is_image(file_path):
|
| 43 |
-
try:
|
| 44 |
-
Image.open(file_path)
|
| 45 |
-
return True
|
| 46 |
-
except IOError:
|
| 47 |
-
return False
|
| 48 |
-
|
| 49 |
-
from supabase import create_client, Client
|
| 50 |
-
|
| 51 |
-
def get_supabase_client():
|
| 52 |
-
url = os.environ.get('supabase_url')
|
| 53 |
-
key = os.environ.get('supbase_key')
|
| 54 |
-
supabase = create_client(url, key)
|
| 55 |
-
return supabase
|
| 56 |
-
|
| 57 |
-
def supabase_insert_message(user_message,response_content,messages,response_data,user_name,user_oauth_token,ip,sign,cookie_value,content_type):
|
| 58 |
-
supabase = get_supabase_client()
|
| 59 |
-
data, count = supabase.table('messages').insert({"user_message": user_message, "response_content": response_content,"messages":messages,"response":response_data,"user_name":user_name,"user_oauth_token":user_oauth_token,"ip":ip,"sign":sign,"cookie":cookie_value,"content_type":content_type}).execute()
|
| 60 |
-
|
| 61 |
-
def supabase_insert_user(name,user_name,profile,picture,oauth_token):
|
| 62 |
-
supabase = get_supabase_client()
|
| 63 |
-
data, count = supabase.table('users').insert({"name":name,"user_name":user_name,"profile":profile,"picture":picture,"oauth_token":oauth_token}).execute()
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
def supabase_fetch_user(user_name):
|
| 67 |
-
supabase = get_supabase_client()
|
| 68 |
-
data,count = supabase.table('users').select("*").eq('user_name',user_name).execute()
|
| 69 |
-
return data
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# def respond(
|
| 74 |
-
# message,
|
| 75 |
-
# history: list[tuple[str, str]],
|
| 76 |
-
# system_message,
|
| 77 |
-
# max_tokens,
|
| 78 |
-
# temperature,
|
| 79 |
-
# top_p,
|
| 80 |
-
# ):
|
| 81 |
-
# messages = [{"role": "system", "content": system_message}]
|
| 82 |
-
|
| 83 |
-
# for val in history:
|
| 84 |
-
# if val[0]:
|
| 85 |
-
# messages.append({"role": "user", "content": val[0]})
|
| 86 |
-
# if val[1]:
|
| 87 |
-
# messages.append({"role": "assistant", "content": val[1]})
|
| 88 |
-
|
| 89 |
-
# messages.append({"role": "user", "content": message})
|
| 90 |
-
|
| 91 |
-
# response = ""
|
| 92 |
-
|
| 93 |
-
# for message in client.chat_completion(
|
| 94 |
-
# messages,
|
| 95 |
-
# max_tokens=max_tokens,
|
| 96 |
-
# stream=True,
|
| 97 |
-
# temperature=temperature,
|
| 98 |
-
# top_p=top_p,
|
| 99 |
-
# ):
|
| 100 |
-
# token = message.choices[0].delta.content
|
| 101 |
-
|
| 102 |
-
# response += token
|
| 103 |
-
# yield response
|
| 104 |
-
|
| 105 |
-
def get_completion(message,history,profile: gr.OAuthProfile | None,oauth_token: gr.OAuthToken | None,request: gr.Request):
|
| 106 |
-
if request:
|
| 107 |
-
ip = request.client.host
|
| 108 |
-
print("Query parameters:", dict(request.query_params))
|
| 109 |
-
sign = dict(request.query_params).get('__sign')
|
| 110 |
-
|
| 111 |
-
# get cookie
|
| 112 |
-
headers = request.headers.raw
|
| 113 |
-
|
| 114 |
-
# find 'cookie'
|
| 115 |
-
cookie_header = next((header for header in headers if header[0] == b'cookie'), None)
|
| 116 |
-
|
| 117 |
-
if cookie_header:
|
| 118 |
-
# extract cookie
|
| 119 |
-
cookie_value = cookie_header[1].decode()
|
| 120 |
-
print(f"Cookie: {cookie_value}")
|
| 121 |
-
else:
|
| 122 |
-
cookie_value = ''
|
| 123 |
-
print("No cookie found in request headers")
|
| 124 |
-
|
| 125 |
-
# check if messages are empty
|
| 126 |
-
if message["text"].strip() == "" and not message["files"]:
|
| 127 |
-
raise gr.Error("Please input a query and optionally image(s).")
|
| 128 |
-
|
| 129 |
-
if message["text"].strip() == "" and message["files"]:
|
| 130 |
-
raise gr.Error("Please input a text query along the image(s).")
|
| 131 |
-
|
| 132 |
-
text = message['text']
|
| 133 |
-
user_message = [
|
| 134 |
-
{"type": "text", "text": text},
|
| 135 |
-
]
|
| 136 |
-
content_type = 'text'
|
| 137 |
-
if message['files']:
|
| 138 |
-
file = message['files'][0]
|
| 139 |
-
public_url = upload_file_to_gcs_blob(file)
|
| 140 |
-
if is_image(file): # only support image file now
|
| 141 |
-
content_image = {
|
| 142 |
-
"type": "image_url",
|
| 143 |
-
"image_url": {
|
| 144 |
-
"url": public_url,
|
| 145 |
-
},}
|
| 146 |
-
user_message.append(content_image)
|
| 147 |
-
content_type = 'image'
|
| 148 |
-
else:
|
| 149 |
-
raise gr.Error("Only support image files now.")
|
| 150 |
-
|
| 151 |
-
history_openai_format = []
|
| 152 |
-
for human, assistant in history:
|
| 153 |
-
# check if there is image info in the history message or empty history messages
|
| 154 |
-
|
| 155 |
-
if isinstance(human, tuple) or human == "" or assistant is None:
|
| 156 |
-
continue
|
| 157 |
-
|
| 158 |
-
history_openai_format.append({"role": "user", "content": human })
|
| 159 |
-
history_openai_format.append({"role": "assistant", "content":assistant})
|
| 160 |
-
history_openai_format.append({"role": "user", "content": user_message})
|
| 161 |
-
# print(history_openai_format)
|
| 162 |
-
|
| 163 |
-
system_message = '''You are Maya, Qspace Corporation's new flagship model that can reason across audio, vision, and text in real time.
|
| 164 |
-
Maya2 matches Maya1 Turbo performance on text in English and code, with significant improvement on text in non-English languages, while also being much faster.
|
| 165 |
-
Maya2 is especially better at vision and audio understanding compared to existing models.
|
| 166 |
-
Maya2's text and image capabilities are avaliable for users now. More capabilities like audio and video will be rolled out iteratively in the future.
|
| 167 |
-
Your default language is the user language.
|
| 168 |
-
'''
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
# headers
|
| 172 |
-
openai_api_key = os.environ.get('openai_api_key')
|
| 173 |
-
base_url = os.environ.get('base_url')
|
| 174 |
-
headers = {
|
| 175 |
-
'Authorization': f'Bearer {openai_api_key}'
|
| 176 |
-
}
|
| 177 |
-
|
| 178 |
-
temperature = 0.7
|
| 179 |
-
max_tokens = 2048
|
| 180 |
-
|
| 181 |
-
init_message = [{"role": "system", "content": system_message}]
|
| 182 |
-
messages = init_message + history_openai_format[-5:] #system message + latest 2 round dialogues + user input
|
| 183 |
-
print(messages)
|
| 184 |
-
# request body
|
| 185 |
-
data = {
|
| 186 |
-
'model': 'Maya', # we use gpt-4o here
|
| 187 |
-
'messages': messages,
|
| 188 |
-
'temperature':temperature,
|
| 189 |
-
'max_tokens':max_tokens,
|
| 190 |
-
'stream':True,
|
| 191 |
-
# 'stream_options':{"include_usage": True}, # retrieving token usage for stream response
|
| 192 |
-
}
|
| 193 |
-
|
| 194 |
-
# get response with stream
|
| 195 |
-
response = requests.post(base_url, headers=headers, json=data,stream=True)
|
| 196 |
-
response_content = ""
|
| 197 |
-
for line in response.iter_lines():
|
| 198 |
-
line = line.decode().strip()
|
| 199 |
-
if line == "data: [DONE]":
|
| 200 |
-
continue
|
| 201 |
-
elif line.startswith("data: "):
|
| 202 |
-
line = line[6:] # remove prefix "data: "
|
| 203 |
-
try:
|
| 204 |
-
data = json.loads(line)
|
| 205 |
-
if "delta" in data["choices"][0]:
|
| 206 |
-
content = data["choices"][0]["delta"].get("content", "")
|
| 207 |
-
response_content += content
|
| 208 |
-
yield response_content
|
| 209 |
-
except json.JSONDecodeError:
|
| 210 |
-
print(f"Error decoding line: {line}")
|
| 211 |
-
|
| 212 |
-
print(response_content)
|
| 213 |
-
print('-----------------------------------\n')
|
| 214 |
-
response_data = {}
|
| 215 |
-
|
| 216 |
-
supabase_insert_message(user_message,response_content,messages,response_data,user_name,user_oauth_token,ip,sign,cookie_value,content_type)
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
"""
|
| 222 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 223 |
-
"""
|
| 224 |
-
|
| 225 |
-
with gr.Blocks(fill_height=True) as demo:
|
| 226 |
-
gr.Markdown(
|
| 227 |
-
"# Maya2"
|
| 228 |
-
"\n\nThis is Maya2, you can use the text and image capabilities now. More capabilities like audio and video will be rolled out iteratively in the future. Stay tuned."
|
| 229 |
-
)
|
| 230 |
-
gr.LoginButton()
|
| 231 |
-
|
| 232 |
-
gr.ChatInterface(
|
| 233 |
-
get_completion,
|
| 234 |
-
multimodal=True,
|
| 235 |
-
# title = title,
|
| 236 |
-
# description = description
|
| 237 |
-
# additional_inputs=[
|
| 238 |
-
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 239 |
-
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 240 |
-
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 241 |
-
# ],
|
| 242 |
-
)
|
| 243 |
-
|
| 244 |
-
demo.queue(default_concurrency_limit=5)
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
if __name__ == "__main__":
|
| 248 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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