Spaces:
Running
Running
File size: 4,356 Bytes
f5258bc e650e3a f5258bc 2195645 f5258bc 2fe8f38 f5258bc 2fe8f38 f5258bc 2fe8f38 f5258bc 2fe8f38 f5258bc 2195645 f5258bc 2195645 f5258bc 2195645 f5258bc 2195645 f5258bc ee175f9 f5258bc 2195645 f5258bc 2195645 f5258bc 2195645 73eca71 2195645 f5258bc 2195645 f5258bc 2195645 73eca71 2195645 f5258bc 2195645 f5258bc ee175f9 f5258bc ee175f9 |
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 145 |
import gradio as gr
import os
from openai import OpenAI
# Initialize OpenAI client
client = OpenAI(
base_url="https://router.huggingface.co/v1",
api_key=os.environ["HF_TOKEN"],
default_headers={
"X-HF-Bill-To": "huggingface"
}
)
def process_message(message, history, image):
"""
Process user message and image, send to the model, and return the response.
"""
# Prepare messages for the API
messages = []
# Add chat history - Gradio 6 uses list of dicts format
for msg_dict in history:
if msg_dict["role"] == "user":
messages.append({"role": "user", "content": msg_dict["content"]})
elif msg_dict["role"] == "assistant":
messages.append({"role": "assistant", "content": msg_dict["content"]})
# Add current message and image
if message or image:
current_message = {"role": "user", "content": []}
if message:
current_message["content"].append({"type": "text", "text": message})
if image:
current_message["content"].append({
"type": "image_url",
"image_url": {"url": image}
})
messages.append(current_message)
# Add user message to history immediately
new_history = history + [{"role": "user", "content": message if message else ""}]
# Get response from the model
response = ""
try:
stream = client.chat.completions.create(
model="zai-org/GLM-4.6V-Flash:zai-org",
messages=messages,
stream=True,
)
# Add assistant message to history
new_history.append({"role": "assistant", "content": ""})
for chunk in stream:
if chunk.choices[0].delta.content:
response += chunk.choices[0].delta.content
# Update the assistant's message
new_history[-1]["content"] = response
yield new_history
except Exception as e:
error_msg = f"Error: {str(e)}"
new_history[-1]["content"] = error_msg
yield new_history
# Create Gradio interface
with gr.Blocks() as demo:
# Add the "Built with anycoder" link
gr.Markdown(
'<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="text-decoration: none;">'
'<span style="color: #4F46E5; font-weight: bold;">Built with anycoder</span>'
'</a>'
)
chatbot = gr.Chatbot(
label="Conversation",
height=400,
avatar_images=(
"https://cdn-icons-png.flaticon.com/512/147/147144.png",
"https://cdn-icons-png.flaticon.com/512/4712/4712025.png"
)
)
with gr.Row():
with gr.Column(scale=3):
msg = gr.Textbox(
label="Your message",
placeholder="Type your message here...",
lines=2
)
with gr.Column(scale=1):
img = gr.Image(
label="Upload image",
type="filepath",
height=150
)
with gr.Row():
submit_btn = gr.Button("Send", variant="primary")
clear_btn = gr.ClearButton(components=[msg, img, chatbot], value="Clear Chat")
# Set up the chat interface
def submit_message(message, history, image):
# Clear the textbox and image after submission
for update in process_message(message, history, image):
yield update
msg.submit(
fn=submit_message,
inputs=[msg, chatbot, img],
outputs=[chatbot],
api_visibility="public"
).then(
lambda: ("", None),
outputs=[msg, img]
)
submit_btn.click(
fn=submit_message,
inputs=[msg, chatbot, img],
outputs=[chatbot],
api_visibility="public"
).then(
lambda: ("", None),
outputs=[msg, img]
)
# Launch the app
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
theme=gr.themes.Soft(primary_hue="blue"),
footer_links=[
{
"label": "Built with anycoder",
"url": "https://huggingface.co/spaces/akhaliq/anycoder"
}
]
) |