|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
import os |
|
|
|
""" |
|
Warning Lamp Detector using Hugging Face Inference API |
|
This application allows users to upload images of warning lamps and get classification results. |
|
""" |
|
|
|
|
|
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") |
|
|
|
def detect_warning_lamp(image, history: list[tuple[str, str]], system_message): |
|
""" |
|
Process the uploaded image and return detection results |
|
""" |
|
|
|
|
|
messages = [{"role": "system", "content": system_message}] |
|
|
|
|
|
messages.append({ |
|
"role": "user", |
|
"content": f"Please analyze this warning lamp image and provide a detailed classification." |
|
}) |
|
|
|
response = "" |
|
for message in client.chat_completion( |
|
messages, |
|
max_tokens=512, |
|
stream=True, |
|
temperature=0.7, |
|
top_p=0.95, |
|
): |
|
token = message.choices[0].delta.content |
|
response += token |
|
yield response |
|
|
|
|
|
with gr.Blocks(title="Warning Lamp Detector", theme=gr.themes.Soft()) as demo: |
|
gr.Markdown(""" |
|
# π¨ Warning Lamp Detector |
|
Upload an image of a warning lamp to get its classification. |
|
|
|
### Instructions: |
|
1. Upload a clear image of the warning lamp |
|
2. Wait for the analysis |
|
3. View the detailed classification results |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
image_input = gr.Image( |
|
label="Upload Warning Lamp Image", |
|
type="pil", |
|
sources="upload" |
|
) |
|
system_message = gr.Textbox( |
|
value="You are an expert in warning lamp classification. Analyze the image and provide detailed information about the type, color, and status of the warning lamp.", |
|
label="System Message", |
|
lines=3 |
|
) |
|
|
|
with gr.Column(scale=1): |
|
chatbot = gr.Chatbot( |
|
[], |
|
elem_id="chatbot", |
|
bubble_full_width=False, |
|
avatar_images=(None, "π¨"), |
|
height=400 |
|
) |
|
|
|
|
|
submit_btn = gr.Button("Analyze Warning Lamp", variant="primary") |
|
submit_btn.click( |
|
detect_warning_lamp, |
|
inputs=[image_input, chatbot, system_message], |
|
outputs=chatbot |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|