Spaces:
Runtime error
Runtime error
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!pip install transformers dataset\n", | |
"!pip install -q gradio\n", | |
"!pip install sentencepiece\n", | |
"!pip install googletrans==3.1.0a0" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Vbo2y6IyU9EE", | |
"outputId": "fba9099a-c64b-4f9e-864f-0c59698a7633" | |
}, | |
"execution_count": 36, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", | |
"Requirement already satisfied: transformers in /usr/local/lib/python3.8/dist-packages (4.26.0)\n", | |
"Requirement already satisfied: dataset in /usr/local/lib/python3.8/dist-packages (1.6.0)\n", | |
"Requirement already satisfied: huggingface-hub<1.0,>=0.11.0 in /usr/local/lib/python3.8/dist-packages (from transformers) (0.12.0)\n", | |
"Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /usr/local/lib/python3.8/dist-packages (from transformers) (0.13.2)\n", | |
"Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.8/dist-packages (from transformers) (1.21.6)\n", | |
"Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.8/dist-packages (from transformers) (4.64.1)\n", | |
"Requirement already satisfied: requests in /usr/local/lib/python3.8/dist-packages (from transformers) (2.25.1)\n", | |
"Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.8/dist-packages (from transformers) (2022.6.2)\n", | |
"Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.8/dist-packages (from transformers) (6.0)\n", | |
"Requirement already satisfied: filelock in /usr/local/lib/python3.8/dist-packages (from transformers) (3.9.0)\n", | |
"Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.8/dist-packages (from transformers) (23.0)\n", | |
"Requirement already satisfied: sqlalchemy<2.0.0,>=1.3.2 in /usr/local/lib/python3.8/dist-packages (from dataset) (1.4.46)\n", | |
"Requirement already satisfied: alembic>=0.6.2 in /usr/local/lib/python3.8/dist-packages (from dataset) (1.9.2)\n", | |
"Requirement already satisfied: banal>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from dataset) (1.0.6)\n", | |
"Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.8/dist-packages (from alembic>=0.6.2->dataset) (6.0.0)\n", | |
"Requirement already satisfied: Mako in /usr/local/lib/python3.8/dist-packages (from alembic>=0.6.2->dataset) (1.2.4)\n", | |
"Requirement already satisfied: importlib-resources in /usr/local/lib/python3.8/dist-packages (from alembic>=0.6.2->dataset) (5.10.2)\n", | |
"Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.8/dist-packages (from huggingface-hub<1.0,>=0.11.0->transformers) (4.4.0)\n", | |
"Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.8/dist-packages (from sqlalchemy<2.0.0,>=1.3.2->dataset) (2.0.2)\n", | |
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.8/dist-packages (from requests->transformers) (1.24.3)\n", | |
"Requirement already satisfied: chardet<5,>=3.0.2 in /usr/local/lib/python3.8/dist-packages (from requests->transformers) (3.0.4)\n", | |
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.8/dist-packages (from requests->transformers) (2.10)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.8/dist-packages (from requests->transformers) (2022.12.7)\n", | |
"Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.8/dist-packages (from importlib-metadata->alembic>=0.6.2->dataset) (3.12.0)\n", | |
"Requirement already satisfied: MarkupSafe>=0.9.2 in /usr/local/lib/python3.8/dist-packages (from Mako->alembic>=0.6.2->dataset) (2.0.1)\n", | |
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", | |
"Requirement already satisfied: sentencepiece in /usr/local/lib/python3.8/dist-packages (0.1.97)\n", | |
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", | |
"Requirement already satisfied: googletrans==3.1.0a0 in /usr/local/lib/python3.8/dist-packages (3.1.0a0)\n", | |
"Requirement already satisfied: httpx==0.13.3 in /usr/local/lib/python3.8/dist-packages (from googletrans==3.1.0a0) (0.13.3)\n", | |
"Requirement already satisfied: idna==2.* in /usr/local/lib/python3.8/dist-packages (from httpx==0.13.3->googletrans==3.1.0a0) (2.10)\n", | |
"Requirement already satisfied: certifi in /usr/local/lib/python3.8/dist-packages (from httpx==0.13.3->googletrans==3.1.0a0) (2022.12.7)\n", | |
"Requirement already satisfied: rfc3986<2,>=1.3 in /usr/local/lib/python3.8/dist-packages (from httpx==0.13.3->googletrans==3.1.0a0) (1.5.0)\n", | |
"Requirement already satisfied: chardet==3.* in /usr/local/lib/python3.8/dist-packages (from httpx==0.13.3->googletrans==3.1.0a0) (3.0.4)\n", | |
"Requirement already satisfied: httpcore==0.9.* in /usr/local/lib/python3.8/dist-packages (from httpx==0.13.3->googletrans==3.1.0a0) (0.9.1)\n", | |
"Requirement already satisfied: sniffio in /usr/local/lib/python3.8/dist-packages (from httpx==0.13.3->googletrans==3.1.0a0) (1.3.0)\n", | |
"Requirement already satisfied: hstspreload in /usr/local/lib/python3.8/dist-packages (from httpx==0.13.3->googletrans==3.1.0a0) (2023.1.1)\n", | |
"Requirement already satisfied: h2==3.* in /usr/local/lib/python3.8/dist-packages (from httpcore==0.9.*->httpx==0.13.3->googletrans==3.1.0a0) (3.2.0)\n", | |
"Requirement already satisfied: h11<0.10,>=0.8 in /usr/local/lib/python3.8/dist-packages (from httpcore==0.9.*->httpx==0.13.3->googletrans==3.1.0a0) (0.9.0)\n", | |
"Requirement already satisfied: hpack<4,>=3.0 in /usr/local/lib/python3.8/dist-packages (from h2==3.*->httpcore==0.9.*->httpx==0.13.3->googletrans==3.1.0a0) (3.0.0)\n", | |
"Requirement already satisfied: hyperframe<6,>=5.2.0 in /usr/local/lib/python3.8/dist-packages (from h2==3.*->httpcore==0.9.*->httpx==0.13.3->googletrans==3.1.0a0) (5.2.0)\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 723 | |
}, | |
"id": "5DNOX2BIU5mO", | |
"outputId": "9311e1ba-5685-4f74-e40a-0f1cd39484d0" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stderr", | |
"text": [ | |
"Using cache found in /root/.cache/torch/hub/pytorch_vision_v0.6.0\n", | |
"/usr/local/lib/python3.8/dist-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.\n", | |
" warnings.warn(\n", | |
"/usr/local/lib/python3.8/dist-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.\n", | |
" warnings.warn(msg)\n" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n", | |
"Note: opening Chrome Inspector may crash demo inside Colab notebooks.\n", | |
"\n", | |
"To create a public link, set `share=True` in `launch()`.\n" | |
] | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Javascript object>" | |
], | |
"application/javascript": [ | |
"(async (port, path, width, height, cache, element) => {\n", | |
" if (!google.colab.kernel.accessAllowed && !cache) {\n", | |
" return;\n", | |
" }\n", | |
" element.appendChild(document.createTextNode(''));\n", | |
" const url = await google.colab.kernel.proxyPort(port, {cache});\n", | |
"\n", | |
" const external_link = document.createElement('div');\n", | |
" external_link.innerHTML = `\n", | |
" <div style=\"font-family: monospace; margin-bottom: 0.5rem\">\n", | |
" Running on <a href=${new URL(path, url).toString()} target=\"_blank\">\n", | |
" https://localhost:${port}${path}\n", | |
" </a>\n", | |
" </div>\n", | |
" `;\n", | |
" element.appendChild(external_link);\n", | |
"\n", | |
" const iframe = document.createElement('iframe');\n", | |
" iframe.src = new URL(path, url).toString();\n", | |
" iframe.height = height;\n", | |
" iframe.allow = \"autoplay; camera; microphone; clipboard-read; clipboard-write;\"\n", | |
" iframe.width = width;\n", | |
" iframe.style.border = 0;\n", | |
" element.appendChild(iframe);\n", | |
" })(7882, \"/\", \"100%\", 500, false, window.element)" | |
] | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [] | |
}, | |
"metadata": {}, | |
"execution_count": 64 | |
} | |
], | |
"source": [ | |
"import gradio as gr\n", | |
"import requests\n", | |
"from PIL import Image\n", | |
"from torchvision import transforms\n", | |
"from transformers import pipeline\n", | |
"import torch\n", | |
"import sentencepiece\n", | |
"import re\n", | |
"import googletrans \n", | |
"from googletrans import Translator\n", | |
"\n", | |
"model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()\n", | |
"\n", | |
"from transformers import SegformerFeatureExtractor, SegformerForImageClassification, T5Tokenizer, T5Model\n", | |
"from PIL import Image\n", | |
"import requests\n", | |
"\n", | |
"\n", | |
"def loadImageToText(image, argument):\n", | |
" # url = \"https://media.istockphoto.com/id/470604022/es/foto/%C3%A1rbol-de-manzano.jpg?s=1024x1024&w=is&k=20&c=R7b6jPeTGsDw75Sqn3VwpNRckqlAkJNPLelb48pCk2U=\"\n", | |
" # image = Image.open(requests.get(url, stream=True).raw)\n", | |
" feature_extractor = SegformerFeatureExtractor.from_pretrained(\"nvidia/mit-b2\")\n", | |
" model = SegformerForImageClassification.from_pretrained(\"nvidia/mit-b2\")\n", | |
" translator = Translator()\n", | |
"\n", | |
" inputs = feature_extractor(images=image, return_tensors=\"pt\")\n", | |
" outputs = model(**inputs)\n", | |
" logits = outputs.logits\n", | |
" # model predicts one of the 1000 ImageNet classes\n", | |
" predicted_class_idx = logits.argmax(-1).item()\n", | |
" part_args = f\"<\"+re.sub(\"[^(\\w|<|>)]+(?=\\w)\", \"><\", argument) + \">\"\n", | |
" story_gen = pipeline(\"text-generation\", \"pranavpsv/gpt2-genre-story-generator\")\n", | |
" story_text = story_gen(part_args + model.config.id2label[predicted_class_idx])\n", | |
"\n", | |
" generate_text_stroy = story_text[0][\"generated_text\"]\n", | |
" ln_text_story = generate_text_stroy[len(part_args):len(generate_text_stroy)]\n", | |
"\n", | |
" translated_ita = translator.translate(ln_text_story, src='en', dest='es')\n", | |
"\n", | |
" return translated_ita.text\n", | |
"\n", | |
"\n", | |
"\n", | |
"#print(loadImageToText(\"animal,super\")) # borra el argumento 'image' y sus variables internas si quieres probarlo desde aquí.\n", | |
"\n", | |
"gr.Interface(fn=loadImageToText,\n", | |
" inputs=[gr.Image(), gr.Text(label=\"Argumentos base\", placeholder=\"Verano, película,playa, superhéroe, animal\")],\n", | |
" outputs=\"text\").launch()\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "zMmh7-ixXNIH" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |