Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import hashlib | |
| import io | |
| import requests | |
| from PIL import Image | |
| import json | |
| def get_image_md5(image_url): | |
| """ | |
| 从URL获取图片,返回其MD5值 | |
| Args: | |
| image_url (str): 图片URL地址 | |
| Returns: | |
| str: JSON格式的结果,包含isSuccess、msg和data字段 | |
| """ | |
| try: | |
| # 从URL获取图片 | |
| response = requests.get(image_url) | |
| response.raise_for_status() | |
| # 打开图片 | |
| image = Image.open(io.BytesIO(response.content)) | |
| # 计算MD5值 | |
| buffered = io.BytesIO() | |
| image.save(buffered, format=image.format) | |
| md5_hash = hashlib.md5(buffered.getvalue()).hexdigest() | |
| result = { | |
| "isSuccess": True, | |
| "msg": "处理成功", | |
| "data": md5_hash | |
| } | |
| return json.dumps(result, ensure_ascii=False, indent=2) | |
| except Exception as e: | |
| result = { | |
| "isSuccess": False, | |
| "msg": f"处理失败: {str(e)}", | |
| "data": None | |
| } | |
| return json.dumps(result, ensure_ascii=False, indent=2) | |
| # 创建Gradio界面 | |
| app = gr.Interface( | |
| fn=get_image_md5, | |
| inputs=gr.Textbox(label="图片URL", placeholder="请输入图片URL地址"), | |
| outputs=gr.Textbox(label="结果 (JSON格式)", lines=10), | |
| title="文子,收你来了", | |
| description="输入图片URL,获取图片的MD5值,返回JSON格式结果" | |
| ) | |
| # 启动应用 | |
| if __name__ == "__main__": | |
| app.launch() |