import torch import gradio as gr from transformers import AutoModel, pipeline, AutoTokenizer inference = pipeline(task="visual-question-answering") def predict(input_img, questions): try: predictions = inference(question=questions, image=input_img) return str(predictions[0]) except Exception as e: # 捕获异常,并将错误信息转换为字符串 error_message = str(e) # 抛出gradio.Error来展示错误弹窗 raise gr.Error(error_message, duration=25) gradio_app = gr.Interface( predict, inputs=[ gr.Image( label="Select A Image", sources=["upload", "webcam"], type="pil" ), "text", ], outputs="text", title="Plz ask my anything", ) if __name__ == "__main__": gradio_app.launch(show_error=True, debug=True)