WWt commited on
Commit
d78aa47
1 Parent(s): dc1419d

Delete 恶劣天气识别部署到gradio.ipynb

Browse files
恶劣天气识别部署到gradio.ipynb DELETED
@@ -1 +0,0 @@
1
- {"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":1665584386249,"user_tz":-480,"elapsed":21161,"user":{"displayName":"金先生","userId":"07679655535683887373"}},"colab":{"base_uri":"https://localhost:8080/"},"outputId":"9ba21fe3-b6d3-4cb4-8d13-98ae8c94931b"},"source":["!pip install -q gradio"],"execution_count":1,"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[K |████████████████████████████████| 5.3 MB 5.3 MB/s \n","\u001b[K |████████████████████████████████| 112 kB 66.5 MB/s \n","\u001b[K |████████████████████████████████| 212 kB 58.1 MB/s \n","\u001b[K |████████████████████████████████| 57 kB 5.5 MB/s \n","\u001b[K |████████████████████████████████| 84 kB 3.6 MB/s \n","\u001b[K |████████████████████████████████| 2.3 MB 41.4 MB/s \n","\u001b[K |████████████████████████████████| 270 kB 75.7 MB/s \n","\u001b[K |████████████████████████████████| 54 kB 3.1 MB/s \n","\u001b[K |████████████████████████████████| 55 kB 3.7 MB/s \n","\u001b[K |████████████████████████████████| 84 kB 3.0 MB/s \n","\u001b[K |████████████████████████████████| 63 kB 2.0 MB/s \n","\u001b[K |████████████████████████████████| 80 kB 8.9 MB/s \n","\u001b[K |████████████████████████████████| 68 kB 6.8 MB/s \n","\u001b[K |████████████████████████████████| 46 kB 4.2 MB/s \n","\u001b[K |████████████████████████████████| 4.1 MB 38.1 MB/s \n","\u001b[K |████████████████████████████████| 593 kB 54.1 MB/s \n","\u001b[K |████████████████████████████████| 856 kB 57.6 MB/s \n","\u001b[?25h Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n"," Building wheel for python-multipart (setup.py) ... \u001b[?25l\u001b[?25hdone\n"]}]},{"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":1665584414521,"user_tz":-480,"elapsed":28277,"user":{"displayName":"金先生","userId":"07679655535683887373"}},"outputId":"3eb19891-35d9-4332-9c63-5a8a347cd34a"},"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["Mounted at /content/drive\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":1665584417575,"user_tz":-480,"elapsed":3059,"user":{"displayName":"金先生","userId":"07679655535683887373"}}},"execution_count":3,"outputs":[]},{"cell_type":"code","metadata":{"id":"e200MmBU2aLT","executionInfo":{"status":"ok","timestamp":1665584417575,"user_tz":-480,"elapsed":4,"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":4,"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":1665584473831,"user_tz":-480,"elapsed":1068,"user":{"displayName":"金先生","userId":"07679655535683887373"}}},"execution_count":6,"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":1665584477391,"user_tz":-480,"elapsed":2,"user":{"displayName":"金先生","userId":"07679655535683887373"}}},"execution_count":7,"outputs":[]},{"cell_type":"code","source":["import gradio as gr\n","git clone https://huggingface.co/spaces/WWt/zz12\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)\n","git add app.py\n","git commit -m \"Add application file\"\n","git push\n"],"metadata":{"id":"8Ls0etEpC1h4","colab":{"base_uri":"https://localhost:8080/","height":134},"executionInfo":{"status":"error","timestamp":1665585158903,"user_tz":-480,"elapsed":455,"user":{"displayName":"金先生","userId":"07679655535683887373"}},"outputId":"2fb94f86-c054-44ad-ed79-0e39eb6177e7"},"execution_count":17,"outputs":[{"output_type":"error","ename":"SyntaxError","evalue":"ignored","traceback":["\u001b[0;36m File \u001b[0;32m\"<ipython-input-17-0a596af2e30b>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m git clone https://huggingface.co/spaces/WWt/zz12\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"]}]},{"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":7,"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":["(<gradio.routes.App at 0x7f9d6e3bfb10>,\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":[]}]}