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