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
text = st.text_area('Enter some text')
|
| 9 |
|
| 10 |
-
# If there is text input, perform sentiment analysis
|
| 11 |
-
if text:
|
| 12 |
-
out = pipe(text)
|
| 13 |
-
# Display the result in JSON format
|
| 14 |
-
st.json(out)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# -*- coding: utf-8 -*-
|
| 5 |
+
"""U.ipynb
|
| 6 |
+
|
| 7 |
+
Automatically generated by Colab.
|
| 8 |
+
|
| 9 |
+
Original file is located at
|
| 10 |
+
https://colab.research.google.com/drive/18U4lnO-dgypXR_aI7ZQXatiMQm8bT7f-
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
pip install -U fastai duckduckgo_search
|
| 18 |
+
|
| 19 |
+
pip install --upgrade duckduckgo_search
|
| 20 |
+
|
| 21 |
+
!pip install duckduckgo_search
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
from duckduckgo_search import DDGS
|
| 28 |
+
|
| 29 |
+
results = DDGS().text("python programming", max_results=5)
|
| 30 |
+
print(results)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
from duckduckgo_search import DDGS
|
| 35 |
+
|
| 36 |
+
from fastcore.all import *
|
| 37 |
+
|
| 38 |
+
def search_images(term, max_images=30):
|
| 39 |
+
print(f"Searching for '{term}'")
|
| 40 |
+
results = DDGS(term, max_results=max_images)
|
| 41 |
+
return L(result['image'] for result in results if 'image' in result)
|
| 42 |
+
|
| 43 |
+
# Example usage
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
from duckduckgo_search import DDGS
|
| 48 |
+
from fastcore.all import *
|
| 49 |
+
|
| 50 |
+
def search_images(term, max_images=5):
|
| 51 |
+
print(f"Searching for images related to '{term}'")
|
| 52 |
+
results = DDGS().images(term, max_results=max_images)
|
| 53 |
+
return L(result['image'] for result in results)
|
| 54 |
+
|
| 55 |
+
# Example usage for bird images
|
| 56 |
+
urls = search_images("bird images", max_images=5)
|
| 57 |
+
print(urls)
|
| 58 |
+
|
| 59 |
+
urls = search_images('bird photos', max_images=1)
|
| 60 |
+
urls[0]
|
| 61 |
+
|
| 62 |
+
from fastdownload import download_url
|
| 63 |
+
dest = 'bird.jpg'
|
| 64 |
+
download_url(urls[0], dest, show_progress=False)
|
| 65 |
+
|
| 66 |
+
from fastai.vision.all import *
|
| 67 |
+
im = Image.open(dest)
|
| 68 |
+
im.to_thumb(256,256)
|
| 69 |
+
|
| 70 |
+
download_url(search_images('forest photos', max_images=1)[0], 'forest.jpg', show_progress=False)
|
| 71 |
+
Image.open('forest.jpg').to_thumb(256,256)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
failed = verify_images(get_image_files(path))
|
| 78 |
+
failed.map(Path.unlink)
|
| 79 |
+
len(failed)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
!pip install -U fastai
|
| 84 |
+
|
| 85 |
+
from fastai.vision.all import *
|
| 86 |
+
|
| 87 |
+
from fastai.data.all import *
|
| 88 |
+
from fastai.vision.all import *
|
| 89 |
+
|
| 90 |
+
searches = 'forest','bird'
|
| 91 |
+
path = Path('bird_or_not')
|
| 92 |
+
from time import sleep
|
| 93 |
+
|
| 94 |
+
for o in searches:
|
| 95 |
+
dest = (path/o)
|
| 96 |
+
dest.mkdir(exist_ok=True, parents=True)
|
| 97 |
+
download_images(dest, urls=search_images(f'{o} photo'))
|
| 98 |
+
sleep(10) # Pause between searches to avoid over-loading server
|
| 99 |
+
download_images(dest, urls=search_images(f'{o} sun photo'))
|
| 100 |
+
sleep(10)
|
| 101 |
+
download_images(dest, urls=search_images(f'{o} shade photo'))
|
| 102 |
+
sleep(10)
|
| 103 |
+
resize_images(path/o, max_size=400, dest=path/o)
|
| 104 |
+
|
| 105 |
+
failed = verify_images(get_image_files(path))
|
| 106 |
+
failed.map(Path.unlink)
|
| 107 |
+
len(failed)
|
| 108 |
+
|
| 109 |
+
is_bird,_,probs = learn.predict(PILImage.create('forest.jpg'))
|
| 110 |
+
print(f"This is a: {is_bird}.")
|
| 111 |
+
print(f"Probability it's a bird: {probs[0]:.4f}")
|
| 112 |
+
|
| 113 |
+
dls = DataBlock(
|
| 114 |
+
blocks=(ImageBlock, CategoryBlock),
|
| 115 |
+
get_items=get_image_files,
|
| 116 |
+
splitter=RandomSplitter(valid_pct=0.2, seed=42),
|
| 117 |
+
get_y=parent_label,
|
| 118 |
+
item_tfms=[Resize(192, method='squish')]
|
| 119 |
+
).dataloaders(path, bs=32)
|
| 120 |
+
|
| 121 |
+
dls.show_batch(max_n=6)
|
| 122 |
+
|
| 123 |
+
learn = vision_learner(dls, resnet18, metrics=error_rate)
|
| 124 |
+
learn.fine_tune(10)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
is_bird,_,probs = learn.predict(PILImage.create('forest.jpg'))
|
| 131 |
+
print(f"This is a: {is_bird}.")
|
| 132 |
+
print(f"Probability it's a bird: {probs[0]:.4f}")
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
learn.export('model6.pkl')
|
| 137 |
+
|
| 138 |
+
from fastai.vision.all import *
|
| 139 |
+
def is_cat(x): return x[0].isupper()
|
| 140 |
+
|
| 141 |
+
img = PILImage.create('forest.jpg')
|
| 142 |
+
img.thumbnail((192,192))
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
img
|
| 147 |
+
|
| 148 |
+
le = load_learner('model6.pkl')
|
| 149 |
+
le.predict(img)
|
| 150 |
+
|
| 151 |
+
img
|
| 152 |
+
|
| 153 |
|
|
|
|
|
|
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|