Skip to content

Commit

Permalink
Run on Modal Labs
Browse files Browse the repository at this point in the history
  • Loading branch information
shomilj committed Jan 12, 2024
1 parent a1c0d2a commit b08d007
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 24 deletions.
53 changes: 33 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@

from flask import escape, jsonify
from typing import Dict, List
from utils import query
from modal import Image, Stub, web_endpoint
from urllib.parse import quote

image = Image.debian_slim().pip_install("pandas").pip_install().copy_local_file("preprocessed.csv")
stub = Stub("cal-admissions-api", image=image)

# For development: python3 -m modal serve main.py
# For production: python3 -m modal deploy main.py

@stub.function()
@web_endpoint(method="POST")
def query_endpoint(data: Dict):
return quote(query(cols=data['columns'], filters=data['filters']).to_csv())

# gcloud functions deploy api --runtime python38 --trigger-http --allow-unauthenticated
def api(request):
"""HTTP Cloud Function.
Args:
request (flask.Request): The request object.
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
"""
request_json = request.get_json(silent=True)
# def api(request):
# """HTTP Cloud Function.
# Args:
# request (flask.Request): The request object.
# <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
# Returns:
# The response text, or any set of values that can be turned into a
# Response object using `make_response`
# <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
# """
# request_json = request.get_json(silent=True)

if request_json and 'columns' in request_json and 'filters' in request_json:
columns, filters = request_json['columns'], request_json['filters']
try:
return escape(query(columns, filters).to_csv())
except Exception as e:
return 'error:' + str(e)
else:
return escape('error: missing parameters')
# if request_json and 'columns' in request_json and 'filters' in request_json:
# columns, filters = request_json['columns'], request_json['filters']
# try:
# return escape(query(columns, filters).to_csv())
# except Exception as e:
# return 'error:' + str(e)
# else:
# return escape('error: missing parameters')
305 changes: 305 additions & 0 deletions query_modal_endpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"id": "a8a18976",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>ethnicity_2</th>\n",
" <th>major</th>\n",
" <th>applied</th>\n",
" <th>admitted</th>\n",
" <th>committed</th>\n",
" <th>admit_rate</th>\n",
" <th>yield_rate</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>African American</td>\n",
" <td>Electrical Eng &amp; Comp Sci</td>\n",
" <td>370.0</td>\n",
" <td>8.0</td>\n",
" <td>4.0</td>\n",
" <td>0.0216</td>\n",
" <td>0.5000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>African American</td>\n",
" <td>L&amp;S Computer Science</td>\n",
" <td>116.0</td>\n",
" <td>19.0</td>\n",
" <td>12.0</td>\n",
" <td>0.1638</td>\n",
" <td>0.6316</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Asian</td>\n",
" <td>Electrical Eng &amp; Comp Sci</td>\n",
" <td>3254.0</td>\n",
" <td>311.0</td>\n",
" <td>159.0</td>\n",
" <td>0.0956</td>\n",
" <td>0.5113</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Asian</td>\n",
" <td>L&amp;S Computer Science</td>\n",
" <td>1497.0</td>\n",
" <td>482.0</td>\n",
" <td>299.0</td>\n",
" <td>0.3220</td>\n",
" <td>0.6203</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Chicano/Latino</td>\n",
" <td>Electrical Eng &amp; Comp Sci</td>\n",
" <td>1235.0</td>\n",
" <td>30.0</td>\n",
" <td>14.0</td>\n",
" <td>0.0243</td>\n",
" <td>0.4667</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Chicano/Latino</td>\n",
" <td>L&amp;S Computer Science</td>\n",
" <td>316.0</td>\n",
" <td>53.0</td>\n",
" <td>31.0</td>\n",
" <td>0.1677</td>\n",
" <td>0.5849</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Decline to State</td>\n",
" <td>Electrical Eng &amp; Comp Sci</td>\n",
" <td>329.0</td>\n",
" <td>54.0</td>\n",
" <td>31.0</td>\n",
" <td>0.1641</td>\n",
" <td>0.5741</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Decline to State</td>\n",
" <td>L&amp;S Computer Science</td>\n",
" <td>184.0</td>\n",
" <td>75.0</td>\n",
" <td>49.0</td>\n",
" <td>0.4076</td>\n",
" <td>0.6533</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>International</td>\n",
" <td>Electrical Eng &amp; Comp Sci</td>\n",
" <td>1575.0</td>\n",
" <td>41.0</td>\n",
" <td>25.0</td>\n",
" <td>0.0260</td>\n",
" <td>0.6098</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>International</td>\n",
" <td>L&amp;S Computer Science</td>\n",
" <td>668.0</td>\n",
" <td>60.0</td>\n",
" <td>38.0</td>\n",
" <td>0.0898</td>\n",
" <td>0.6333</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>Native American/Alaska Native</td>\n",
" <td>Electrical Eng &amp; Comp Sci</td>\n",
" <td>34.0</td>\n",
" <td>2.0</td>\n",
" <td>1.0</td>\n",
" <td>0.0588</td>\n",
" <td>0.5000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>Native American/Alaska Native</td>\n",
" <td>L&amp;S Computer Science</td>\n",
" <td>15.0</td>\n",
" <td>5.0</td>\n",
" <td>1.0</td>\n",
" <td>0.3333</td>\n",
" <td>0.2000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>Pacific Islander</td>\n",
" <td>Electrical Eng &amp; Comp Sci</td>\n",
" <td>26.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0000</td>\n",
" <td>0.0000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>Pacific Islander</td>\n",
" <td>L&amp;S Computer Science</td>\n",
" <td>6.0</td>\n",
" <td>2.0</td>\n",
" <td>0.0</td>\n",
" <td>0.3333</td>\n",
" <td>0.0000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>White</td>\n",
" <td>Electrical Eng &amp; Comp Sci</td>\n",
" <td>1613.0</td>\n",
" <td>83.0</td>\n",
" <td>36.0</td>\n",
" <td>0.0515</td>\n",
" <td>0.4337</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>White</td>\n",
" <td>L&amp;S Computer Science</td>\n",
" <td>628.0</td>\n",
" <td>154.0</td>\n",
" <td>64.0</td>\n",
" <td>0.2452</td>\n",
" <td>0.4156</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" ethnicity_2 major applied \\\n",
"0 African American Electrical Eng & Comp Sci 370.0 \n",
"1 African American L&S Computer Science 116.0 \n",
"2 Asian Electrical Eng & Comp Sci 3254.0 \n",
"3 Asian L&S Computer Science 1497.0 \n",
"4 Chicano/Latino Electrical Eng & Comp Sci 1235.0 \n",
"5 Chicano/Latino L&S Computer Science 316.0 \n",
"6 Decline to State Electrical Eng & Comp Sci 329.0 \n",
"7 Decline to State L&S Computer Science 184.0 \n",
"8 International Electrical Eng & Comp Sci 1575.0 \n",
"9 International L&S Computer Science 668.0 \n",
"10 Native American/Alaska Native Electrical Eng & Comp Sci 34.0 \n",
"11 Native American/Alaska Native L&S Computer Science 15.0 \n",
"12 Pacific Islander Electrical Eng & Comp Sci 26.0 \n",
"13 Pacific Islander L&S Computer Science 6.0 \n",
"14 White Electrical Eng & Comp Sci 1613.0 \n",
"15 White L&S Computer Science 628.0 \n",
"\n",
" admitted committed admit_rate yield_rate \n",
"0 8.0 4.0 0.0216 0.5000 \n",
"1 19.0 12.0 0.1638 0.6316 \n",
"2 311.0 159.0 0.0956 0.5113 \n",
"3 482.0 299.0 0.3220 0.6203 \n",
"4 30.0 14.0 0.0243 0.4667 \n",
"5 53.0 31.0 0.1677 0.5849 \n",
"6 54.0 31.0 0.1641 0.5741 \n",
"7 75.0 49.0 0.4076 0.6533 \n",
"8 41.0 25.0 0.0260 0.6098 \n",
"9 60.0 38.0 0.0898 0.6333 \n",
"10 2.0 1.0 0.0588 0.5000 \n",
"11 5.0 1.0 0.3333 0.2000 \n",
"12 0.0 0.0 0.0000 0.0000 \n",
"13 2.0 0.0 0.3333 0.0000 \n",
"14 83.0 36.0 0.0515 0.4337 \n",
"15 154.0 64.0 0.2452 0.4156 "
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import html\n",
"import requests, io\n",
"import pandas as pd\n",
"from urllib.parse import unquote\n",
"\n",
"url = 'https://shomilj--cal-admissions-api-query-endpoint.modal.run'\n",
"\n",
"content = requests.post(url, json={\n",
" 'filters': {\n",
" 'year': ['2017-18'],\n",
" 'major': ['Electrical Eng & Comp Sci', 'L&S Computer Science'],\n",
" },\n",
" 'columns': ['ethnicity_2', 'major']\n",
"}).text\n",
"\n",
"content = unquote(content)[1:-1]\n",
"pd.read_csv(io.StringIO(content), index_col=0).reset_index(drop=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99ad3280",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc2e22cb",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
7 changes: 3 additions & 4 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import pandas as pd

def load():
df = pd.read_csv('preprocessed.csv', index_col=0)
df = pd.read_csv('/preprocessed.csv', index_col=0)
drop_cols = ["('Applied')", "('Admitted')", "('SIRed')"]
applied = df[~pd.isna(df["('Applied')"])].drop(columns=drop_cols)
admitted = df[~pd.isna(df["('Admitted')"])].drop(columns=drop_cols)
committed = df[~pd.isna(df["('SIRed')"])].drop(columns=drop_cols)
return applied, admitted, committed

applied, admitted, committed = load()

def group(data, categories):
filtered = data.copy()
filtered['admit_rate'] = filtered['admit_rate'] * filtered['count']
Expand Down Expand Up @@ -45,7 +43,8 @@ def query(cols, filters={}):
'major': ['Computer Science']]
]
"""

applied, admitted, committed = load()

dss = [applied, admitted, committed]
results = []
for ds in dss:
Expand Down

0 comments on commit b08d007

Please sign in to comment.