Spaces:
Running
Running
Commit
·
b2f8372
1
Parent(s):
dd55947
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,147 +1,78 @@
|
|
| 1 |
-
import io
|
| 2 |
-
import random
|
| 3 |
-
from typing import List, Tuple
|
| 4 |
|
| 5 |
-
import
|
|
|
|
| 6 |
import panel as pn
|
| 7 |
-
from PIL import Image
|
| 8 |
-
from transformers import CLIPModel, CLIPProcessor
|
| 9 |
|
| 10 |
-
pn.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
"brand-twitter": "https://twitter.com/Panel_Org",
|
| 15 |
-
"brand-linkedin": "https://www.linkedin.com/company/panel-org",
|
| 16 |
-
"message-circle": "https://discourse.holoviz.org/",
|
| 17 |
-
"brand-discord": "https://discord.gg/AXRHnJU6sP",
|
| 18 |
-
}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
api_url = f"https://api.the{pet}api.com/v1/images/search"
|
| 24 |
-
async with aiohttp.ClientSession() as session:
|
| 25 |
-
async with session.get(api_url) as resp:
|
| 26 |
-
return (await resp.json())[0]["url"]
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
async with aiohttp.ClientSession() as session:
|
| 40 |
-
async with session.get(image_url) as resp:
|
| 41 |
-
return Image.open(io.BytesIO(await resp.read()))
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
def get_similarity_scores(class_items: List[str], image: Image) -> List[float]:
|
| 45 |
-
processor, model = load_processor_model(
|
| 46 |
-
"openai/clip-vit-base-patch32", "openai/clip-vit-base-patch32"
|
| 47 |
-
)
|
| 48 |
-
inputs = processor(
|
| 49 |
-
text=class_items,
|
| 50 |
-
images=[image],
|
| 51 |
-
return_tensors="pt", # pytorch tensors
|
| 52 |
-
)
|
| 53 |
-
outputs = model(**inputs)
|
| 54 |
-
logits_per_image = outputs.logits_per_image
|
| 55 |
-
class_likelihoods = logits_per_image.softmax(dim=1).detach().numpy()
|
| 56 |
-
return class_likelihoods[0]
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
async def process_inputs(class_names: List[str], image_url: str):
|
| 60 |
-
"""
|
| 61 |
-
High level function that takes in the user inputs and returns the
|
| 62 |
-
classification results as panel objects.
|
| 63 |
-
"""
|
| 64 |
-
try:
|
| 65 |
-
main.disabled = True
|
| 66 |
-
if not image_url:
|
| 67 |
-
yield "##### ⚠️ Provide an image URL"
|
| 68 |
-
return
|
| 69 |
-
|
| 70 |
-
yield "##### ⚙ Fetching image and running model..."
|
| 71 |
-
try:
|
| 72 |
-
pil_img = await open_image_url(image_url)
|
| 73 |
-
img = pn.pane.Image(pil_img, height=400, align="center")
|
| 74 |
-
except Exception as e:
|
| 75 |
-
yield f"##### 😔 Something went wrong, please try a different URL!"
|
| 76 |
-
return
|
| 77 |
-
|
| 78 |
-
class_items = class_names.split(",")
|
| 79 |
-
class_likelihoods = get_similarity_scores(class_items, pil_img)
|
| 80 |
-
|
| 81 |
-
# build the results column
|
| 82 |
-
results = pn.Column("##### 🎉 Here are the results!", img)
|
| 83 |
-
|
| 84 |
-
for class_item, class_likelihood in zip(class_items, class_likelihoods):
|
| 85 |
-
row_label = pn.widgets.StaticText(
|
| 86 |
-
name=class_item.strip(), value=f"{class_likelihood:.2%}", align="center"
|
| 87 |
-
)
|
| 88 |
-
row_bar = pn.indicators.Progress(
|
| 89 |
-
value=int(class_likelihood * 100),
|
| 90 |
-
sizing_mode="stretch_width",
|
| 91 |
-
bar_color="secondary",
|
| 92 |
-
margin=(0, 10),
|
| 93 |
-
design=pn.theme.Material,
|
| 94 |
-
)
|
| 95 |
-
results.append(pn.Column(row_label, row_bar))
|
| 96 |
-
yield results
|
| 97 |
-
finally:
|
| 98 |
-
main.disabled = False
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
# create widgets
|
| 102 |
-
randomize_url = pn.widgets.Button(name="Randomize URL", align="end")
|
| 103 |
-
|
| 104 |
-
image_url = pn.widgets.TextInput(
|
| 105 |
-
name="Image URL to classify",
|
| 106 |
-
value=pn.bind(random_url, randomize_url),
|
| 107 |
-
)
|
| 108 |
-
class_names = pn.widgets.TextInput(
|
| 109 |
-
name="Comma separated class names",
|
| 110 |
-
placeholder="Enter possible class names, e.g. cat, dog",
|
| 111 |
-
value="cat, dog, parrot",
|
| 112 |
-
)
|
| 113 |
|
| 114 |
-
input_widgets = pn.Column(
|
| 115 |
-
"##### 😊 Click randomize or paste a URL to start classifying!",
|
| 116 |
-
pn.Row(image_url, randomize_url),
|
| 117 |
-
class_names,
|
| 118 |
-
)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
href_button.js_on_click(code=f"window.open('{url}')")
|
| 131 |
-
footer_row.append(href_button)
|
| 132 |
-
footer_row.append(pn.Spacer())
|
| 133 |
-
|
| 134 |
-
# create dashboard
|
| 135 |
-
main = pn.WidgetBox(
|
| 136 |
-
input_widgets,
|
| 137 |
-
interactive_result,
|
| 138 |
-
footer_row,
|
| 139 |
)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import yfinance as yf
|
| 4 |
import panel as pn
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
@pn.cache
|
| 7 |
+
def get_df(ticker, startdate , enddate , interval="1d"):
|
| 8 |
+
# interval="1d"
|
| 9 |
+
# get_df(ticker ="PG", startdate="2000-01-01" , enddate="2023-09-01" , interval="1d")
|
| 10 |
+
DF = yf.Ticker(ticker).history(start=startdate,end=enddate,interval=interval)
|
| 11 |
+
DF['SMA50'] = DF.Close.rolling(window=50).mean()
|
| 12 |
+
DF = DF.reset_index()
|
| 13 |
+
return DF
|
| 14 |
|
| 15 |
+
def get_hvplot(ticker , startdate , enddate , interval):
|
| 16 |
+
DF = get_df(ticker , startdate=startdate , enddate=enddate , interval=interval)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
import hvplot.pandas # Ensure hvplot is installed (pip install hvplot)
|
| 19 |
+
from sklearn.linear_model import LinearRegression
|
| 20 |
+
import holoviews as hv
|
| 21 |
+
hv.extension('bokeh')
|
| 22 |
+
# Assuming your dataframe is named 'df' with columns 'Date' and 'Close'
|
| 23 |
+
# If not, replace 'Date' and 'Close' with your actual column names.
|
| 24 |
|
| 25 |
+
# Step 1: Create a scatter plot using hvplot
|
| 26 |
+
scatter_plot = DF.hvplot(x='Date', y='Close', kind='scatter',title=f'{ticker} Close vs. Date')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Step 2: Fit a linear regression model
|
| 29 |
+
DF['Date2'] = pd.to_numeric(DF['Date'])
|
| 30 |
+
X = DF[['Date2']]
|
| 31 |
+
y = DF[['Close']] #.values
|
| 32 |
+
model = LinearRegression().fit(X, y)
|
| 33 |
|
| 34 |
+
# # Step 3: Predict using the linear regression model
|
| 35 |
+
DF['Predicted_Close'] = model.predict(X)
|
| 36 |
+
|
| 37 |
+
# # Step 4: Create a line plot for linear regression
|
| 38 |
+
line_plot = DF.hvplot(x='Date', y='Predicted_Close', kind='line',line_dash='dashed', color='red')
|
| 39 |
+
|
| 40 |
+
# # Step 5: Overlay scatter plot and linear regression line
|
| 41 |
+
# return (scatter_plot * line_plot).opts(width=800, height=600, show_grid=True, gridstyle={ 'grid_line_color': 'gray'})
|
| 42 |
+
# grid_style = {'grid_line_color': 'black'}#, 'grid_line_width': 1.5, 'ygrid_bounds': (0.3, 0.7),'minor_xgrid_line_color': 'lightgray', 'xgrid_line_dash': [4, 4]}
|
| 43 |
+
return (scatter_plot * line_plot).opts(width=800, height=600, show_grid=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
import panel as pn
|
| 47 |
+
from datetime import datetime
|
| 48 |
+
from datetime import date
|
| 49 |
+
pn.extension('bokeh', template='bootstrap')
|
| 50 |
+
import hvplot.pandas
|
| 51 |
+
|
| 52 |
+
tickers = ['AAPL', 'META', 'GOOG', 'IBM', 'MSFT','NKE','DLTR','DG']
|
| 53 |
+
|
| 54 |
+
ticker = pn.widgets.Select(name='Ticker', options=tickers)
|
| 55 |
+
window = pn.widgets.IntSlider(name='Window Size', value=50, start=1, end=200, step=5)
|
| 56 |
+
|
| 57 |
+
# Create a DatePicker widget with a minimum date of 2000-01-01
|
| 58 |
+
date_start = pn.widgets.DatePicker(
|
| 59 |
+
name ="Start Date",
|
| 60 |
+
description='Select a Date',
|
| 61 |
+
start= date(2000, 1, 1)
|
| 62 |
)
|
| 63 |
|
| 64 |
+
date_end = pn.widgets.DatePicker(
|
| 65 |
+
name ="End Date",# value=datetime(2000, 1, 1),
|
| 66 |
+
description='Select a Date',
|
| 67 |
+
end= date(2023, 9, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
+
date_start.value = date(2010,1,1)
|
| 71 |
+
date_end.value = date.today()
|
| 72 |
+
|
| 73 |
+
pn.Row(
|
| 74 |
+
pn.Column( ticker, window , date_start , date_end),
|
| 75 |
+
# pn.panel(pn.bind(get_hvplot, ticker, "2010-01-01","2023-09-01","1d")) #, sizing_mode='stretch_width')
|
| 76 |
+
pn.panel(pn.bind(get_hvplot, ticker, date_start , date_end,"1d")) #, sizing_mode='stretch_width')
|
| 77 |
+
).servable(title="Under Valued Screener- Linear Regression")
|
| 78 |
+
|