Spaces:
Runtime error
Runtime error
File size: 4,409 Bytes
32cf150 9cdf772 32cf150 9cdf772 32cf150 9cdf772 32cf150 9cdf772 79107cf 9cdf772 2a23de3 9cdf772 32cf150 9cdf772 b3ff4da 32cf150 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
import gradio as gr
from huggingface_hub import InferenceClient
"""
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
"""
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
from google.cloud import storage
from google.oauth2 import service_account
import json
# upload image to google cloud storage
def upload_image_to_gcs_blob(image):
google_creds = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
creds_json = json.loads(google_creds)
credentials = service_account.Credentials.from_service_account_info(creds_json)
# 现在您可以使用这些凭证对Google Cloud服务进行认证
storage_client = storage.Client(credentials=credentials, project=creds_json['project_id'])
bucket_name=os.environ.get('bucket_name')
bucket = storage_client.bucket(bucket_name)
destination_blob_name = os.path.basename(image)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(image)
public_url = blob.public_url
return public_url
# def respond(
# message,
# history: list[tuple[str, str]],
# system_message,
# max_tokens,
# temperature,
# top_p,
# ):
# messages = [{"role": "system", "content": system_message}]
# for val in history:
# if val[0]:
# messages.append({"role": "user", "content": val[0]})
# if val[1]:
# messages.append({"role": "assistant", "content": val[1]})
# messages.append({"role": "user", "content": message})
# response = ""
# for message in client.chat_completion(
# messages,
# max_tokens=max_tokens,
# stream=True,
# temperature=temperature,
# top_p=top_p,
# ):
# token = message.choices[0].delta.content
# response += token
# yield response
def get_completion(message,history,system_message,max_tokens,temperature):
# base64_image = encode_image(image)
if message["text"].strip() == "" and not message["files"]:
gr.Error("Please input a query and optionally image(s).")
if message["text"].strip() == "" and message["files"]:
gr.Error("Please input a text query along the image(s).")
text = message['text']
content = [
{"type": "text", "text": text},
]
if message['files']:
image = message['files'][0]
image_url = upload_image_to_gcs_blob(image)
content_image = {
"type": "image_url",
"image_url": {
"url": image_url,
},}
content.append(content_image)
init_message = [{"role": "system", "content": system_message}]
history_openai_format = []
for human, assistant in history:
history_openai_format.append({"role": "user", "content": human })
history_openai_format.append({"role": "assistant", "content":assistant})
history_openai_format.append({"role": "user", "content": content})
# 请求头部信息
openai_api_key = os.environ.get('openai_api_key')
headers = {
'Authorization': f'Bearer {openai_api_key}'
}
# 请求体信息
data = {
'model': 'gpt-4o', # 可以根据需要更换其他模型
'messages': init_message + history_openai_format[-5:], #system message + 最近的2次對話 + 最新一條消息
'temperature': temperature, # 可以根据需要调整
'max_tokens':max_tokens,
# 'stream':True,
}
response = requests.post('https://burn.hair/v1/chat/completions', headers=headers, json=data)
# 解析响应内容
response_data = response.json()
response_content = response_data['choices'][0]['message']['content']
usage = response_data['usage']
return response_content
"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
demo = gr.ChatInterface(
get_completion,
multimodal=True,
additional_inputs=[
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
],
)
if __name__ == "__main__":
demo.launch() |