{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"gpuClass":"standard"},"cells":[{"cell_type":"markdown","metadata":{"id":"D_XJcxqx22uj"},"source":["## Using Gradio to create a simple interface.\n","\n","Check out the library on [github](https://github.com/gradio-app/gradio-UI) and see the [getting started](https://gradio.app/getting_started.html) page for more demos."]},{"cell_type":"markdown","metadata":{"id":"AUHtJ20jYqd9"},"source":["We'll start with a basic function that greets an input name."]},{"cell_type":"markdown","metadata":{"id":"R06dbZZaYJDq"},"source":["Now we'll wrap this function with a Gradio interface."]},{"cell_type":"code","metadata":{"id":"fJUJLWQ92g6R","executionInfo":{"status":"ok","timestamp":1665586405793,"user_tz":-480,"elapsed":4526,"user":{"displayName":"金先生","userId":"07679655535683887373"}}},"source":["!pip install -q gradio"],"execution_count":24,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"uQheRaw5YVTL"},"source":["That's all! Go ahead and open that share link in a new tab. Check out our [getting started](https://gradio.app/getting_started.html) page for more complicated demos."]},{"cell_type":"code","source":["from google.colab import drive\n","drive.mount('/content/drive')"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"KlZeF5loRGRe","executionInfo":{"status":"ok","timestamp":1665586415670,"user_tz":-480,"elapsed":2922,"user":{"displayName":"金先生","userId":"07679655535683887373"}},"outputId":"b7fd8261-dbe6-41b4-eddb-3ae1163a83b2"},"execution_count":25,"outputs":[{"output_type":"stream","name":"stdout","text":["Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"]}]},{"cell_type":"code","source":["from keras.datasets import mnist\n","from keras.utils import np_utils\n","from keras.models import Sequential\n","from keras.layers import Dense,Activation"],"metadata":{"id":"4OpspV6iV1wS","executionInfo":{"status":"ok","timestamp":1665586417692,"user_tz":-480,"elapsed":1,"user":{"displayName":"金先生","userId":"07679655535683887373"}}},"execution_count":26,"outputs":[]},{"cell_type":"code","metadata":{"id":"e200MmBU2aLT","executionInfo":{"status":"ok","timestamp":1665586421228,"user_tz":-480,"elapsed":3,"user":{"displayName":"金先生","userId":"07679655535683887373"}}},"source":["# import gradio\n","# gradio.Interface(greet, \"text\", \"text\").launch(share=True)\n","from keras.models import load_model\n","#import tensorflow as tf\n","# 替换自己的模型\n","#inception_net = tf.keras.applications.MobileNetV2()\n"],"execution_count":27,"outputs":[]},{"cell_type":"code","source":["from keras.models import load_model\n","import numpy as np\n","from keras.preprocessing import image\n","from keras.preprocessing.image import load_img\n","from PIL import ImageFilter\n","from PIL import Image\n","import cv2\n","import os\n","\n","#PHOTO_SIZE = 120 #对图片大小进行限制,低于120KB的抛弃\n","#path = r'/content/sunny.jpg' # 目标\n","weight_path = r'/content/drive/MyDrive/恶劣天气图像识别代码-毕业设计-戴一凡/basic.h5' #权重文件\n","\n","#re_x =256 #输入到神经网络中的文件的大小\n","\n","model = load_model(weight_path)\n","#img = Image.open(path)\n","#img = img.resize((re_x,re_x), Image.ANTIALIAS) #缩放到事先指定的大小\n","#img = np.expand_dims(img, axis=0)\n","#print(img)\n","#inception_net=model.predict(img)#获取预测值\n"],"metadata":{"id":"V1H-DJgUZ3YX","executionInfo":{"status":"ok","timestamp":1665586434870,"user_tz":-480,"elapsed":1729,"user":{"displayName":"金先生","userId":"07679655535683887373"}}},"execution_count":28,"outputs":[]},{"cell_type":"code","source":["#labels =['Srainy', 'Mrainy', 'Lrainy', 'Sfoggy', 'Mfoggy','Lfoggy','sunny']\n","labels=['天气:小雨, 降雨强度(mm/min):0.8, 能见度(m):500, 道路摩擦系数:0.64, 限速(km/h):80',\n"," '天气:中雨, 降雨强度(mm/min):1.2, 能见度(m):250, 道路摩擦系数:0.58, 限速(km/h):50',\n"," '天气:大雨, 降雨强度(mm/min):1.6, 能见度(m):50-150, 道路摩擦系数:0.45, 限速(km/h):30',\n"," '天气:薄雾, 降雨强度(mm/min):0, 能见度(m):500:, 道路摩擦系数:0.8, 限速(km/h):110',\n"," '天气:大雾, 降雨强度(mm/min):0, 能见度(m):200, 道路摩擦系数:0.8, 限速(km/h):70',\n"," '天气:浓雾, 降雨强度(mm/min):0:, 能见度(m):50-100, 道路摩擦系数:0.8, 限速(km/h):40',\n"," '天气:晴, 降雨强度(mm/min):0, 能见度(m):>1000:, 道路摩擦系数:0.8, 限速(km/h):120']\n","def classify_image(inp):\n"," inp = inp.resize((256,256), Image.ANTIALIAS) #缩放到事先指定的大小\n"," inp = np.expand_dims(inp, axis=0)\n"," prediction = model.predict(inp)\n"," confidences = {labels[i]: float(prediction[0][i]) for i in range(7)}\n"," return confidences\n","#path = r'/content/rainy.jpg'\n","\n","#img = Image.open(path)\n","#print(classify_image(img))"],"metadata":{"id":"Og4_R6lN49al","executionInfo":{"status":"ok","timestamp":1665586436812,"user_tz":-480,"elapsed":1,"user":{"displayName":"金先生","userId":"07679655535683887373"}}},"execution_count":29,"outputs":[]},{"cell_type":"code","source":["import gradio as gr\n","gr.Interface(\n"," fn=classify_image,\n"," inputs=gr.Image(type=\"pil\",shape=(256,256)),\n"," outputs=gr.Label(num_top_classes=1),\n"," examples=[\"rainy.jpg\", \"sunny.jpg\"],\n"," interpretation=\"default\",cache_examples=True,title=\"恶劣天气图像识别与预警\"\n",").launch(enable_queue=True)"],"metadata":{"id":"8Ls0etEpC1h4","colab":{"base_uri":"https://localhost:8080/","height":660},"executionInfo":{"status":"ok","timestamp":1665587091024,"user_tz":-480,"elapsed":9765,"user":{"displayName":"金先生","userId":"07679655535683887373"}},"outputId":"c70b2110-b366-4116-92e0-3cbde69eece0"},"execution_count":36,"outputs":[{"output_type":"stream","name":"stdout","text":["Caching examples at: '/content/gradio_cached_examples/66/log.csv'\n","Colab notebook detected. To show errors in colab notebook, set `debug=True` in `launch()`\n","Running on public URL: https://18698.gradio.app\n","\n","This share link expires in 72 hours. For free permanent hosting, check out Spaces: https://huggingface.co/spaces\n"]},{"output_type":"display_data","data":{"text/plain":[""],"text/html":["
"]},"metadata":{}},{"output_type":"execute_result","data":{"text/plain":["(,\n"," 'http://127.0.0.1:7862/',\n"," 'https://18698.gradio.app')"]},"metadata":{},"execution_count":36}]},{"cell_type":"code","source":["import gradio as gr\n","def generate_mutimodal(title, context, senti, num, img):\n"," return f\"Title:{title}\\nContext:{context}\\nSenti:{senti}\\nNum:{num}...{img}\"\n"," \n","server = gr.Interface(\n"," fn=generate_mutimodal, \n"," inputs=[\n"," gr.Textbox(lines=1, placeholder=\"请输入标题\", label=\"标题\", interactive=True), # interactive指定该组件是否允许用户输入或者交互\n"," gr.Textbox(lines=2, placeholder=\"请输入正文\", label=\"正文\"),\n"," gr.Radio([\"0\", \"1\", \"随机\"], label=\"倾向\"),\n"," gr.Number(label='条数'),\n"," gr.Image(shape=(200, 200), label=\"请上传图片(可选)\").style(height='24', rounded=True)\n"," ], \n"," outputs=\"text\",\n"," examples=[\n"," [\"标题1\", \"正文1\", \"0\", None],\n"," [\"标题2\", \"正文2\", \"随机\"],\n"," ],\n"," title=\"可控文本生成\",\n"," description=\"标题和正文至少一个非空,图片可选是否上传\",\n"," article=\"\"\"\n"," ### 这里是底部的article\n"," #### article 1\n"," This is article 1\n"," \"\"\",\n"," css=\"body {background-color: red; font-size: large}\",\n"," allow_flagging=\"never\", # 输出侧是否开启flag功能 比如让用户去标记输出结果是否正确 写到日志\n"," live=False, # live: 用户输入改变时是否自动计算结果\n"," cache_examples=False, # cache_examples: 是否将example里的结果提前算好存到缓存里\n",")\n","\n","server.launch(\n"," enable_queue=False, # enable_queue: 如果app流量很大,需设置参数以防止超时。这将使用长轮询对调用进行排队,一次仅处理一个调用。长轮询还可以防止网络超时,因此,如果函数的推理时间很长(> 1 分钟),则应使用队列。\n"," auth=(\"admin\", \"admin\"), # auth: 参数值可以是 (user, pwd) 的元组,也可以是一个函数名(用户名和密码是参数, 并返回 True/False)。enable_queue=True 时,auth不能传元组。\n"," share=True # share: 可生成 72h 的公网访问地址,原理相当于 gradio 提供了一个内网穿透的代理,并没有把代码+环境上传到他们服务器\n",")\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"h0SZKbaerIST","executionInfo":{"status":"ok","timestamp":1665552816914,"user_tz":-480,"elapsed":5630,"user":{"displayName":"金先生","userId":"07679655535683887373"}},"outputId":"6e12b243-6d41-4fcb-ecce-c27255a0b8f7"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.7/dist-packages/gradio/components.py:197: UserWarning: 'rounded' styling is no longer supported. To round adjacent components together, place them in a Column(variant='box').\n"," \"'rounded' styling is no longer supported. To round adjacent components together, place them in a Column(variant='box').\"\n"]},{"output_type":"stream","name":"stdout","text":["Colab notebook detected. To show errors in colab notebook, set `debug=True` in `launch()`\n","Running on public URL: https://17915.gradio.app\n","\n","This share link expires in 72 hours. For free permanent hosting, check out Spaces: https://huggingface.co/spaces\n"]},{"output_type":"execute_result","data":{"text/plain":["(,\n"," 'http://127.0.0.1:7861/',\n"," 'https://17915.gradio.app')"]},"metadata":{},"execution_count":7}]},{"cell_type":"code","source":[],"metadata":{"id":"t86gBSOLMFmX"},"execution_count":null,"outputs":[]}]}