Spaces:
Runtime error
Runtime error
Commit
·
dcca006
1
Parent(s):
1bb9e65
Dev & Prod
Browse files- .github/workflows/main.yml +44 -3
- inference.py +59 -0
.github/workflows/main.yml
CHANGED
|
@@ -10,17 +10,58 @@ env:
|
|
| 10 |
api_secret: ${{ secrets.HF }}
|
| 11 |
|
| 12 |
jobs:
|
| 13 |
-
|
| 14 |
runs-on: ubuntu-latest
|
| 15 |
steps:
|
| 16 |
- uses: actions/checkout@v2
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
with:
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
- name: Add remote
|
| 20 |
env:
|
| 21 |
HF: ${{ secrets.HF }}
|
| 22 |
run: git remote add space https://cccmatthew:$HF@huggingface.co/spaces/cccmatthew/gp30-deployment
|
| 23 |
-
- name:
|
| 24 |
env:
|
| 25 |
HF: ${{ secrets.HF }}
|
| 26 |
run: git push --force https://cccmatthew:$HF@huggingface.co/spaces/cccmatthew/gp30-deployment main
|
|
|
|
| 10 |
api_secret: ${{ secrets.HF }}
|
| 11 |
|
| 12 |
jobs:
|
| 13 |
+
Build:
|
| 14 |
runs-on: ubuntu-latest
|
| 15 |
steps:
|
| 16 |
- uses: actions/checkout@v2
|
| 17 |
+
|
| 18 |
+
# Set up Python 3.11 environment
|
| 19 |
+
- name: Set up Python 3.11
|
| 20 |
+
uses: actions/setup-python@v4
|
| 21 |
with:
|
| 22 |
+
python-version: "3.11"
|
| 23 |
+
|
| 24 |
+
# Install dependencies
|
| 25 |
+
- name: Install dependencies
|
| 26 |
+
run: |
|
| 27 |
+
python -m pip install --upgrade pip
|
| 28 |
+
pip install -e ".[dev]"
|
| 29 |
+
|
| 30 |
+
- name: Model Testing
|
| 31 |
+
run: |
|
| 32 |
+
python3 inference.py
|
| 33 |
+
|
| 34 |
+
DeployDev:
|
| 35 |
+
name: Deploy to Dev
|
| 36 |
+
if: github.event_name == 'pull_request'
|
| 37 |
+
needs: [Build]
|
| 38 |
+
runs-on: ubuntu-latest
|
| 39 |
+
environment:
|
| 40 |
+
name: Dev
|
| 41 |
+
url: 'https://huggingface.co/spaces/cccmatthew/gp30-dev'
|
| 42 |
+
steps:
|
| 43 |
+
- name: Add remote
|
| 44 |
+
env:
|
| 45 |
+
HF: ${{ secrets.HF }}
|
| 46 |
+
run: git remote add space https://cccmatthew:$HF@huggingface.co/spaces/cccmatthew/gp30-dev
|
| 47 |
+
- name: Deploy
|
| 48 |
+
env:
|
| 49 |
+
HF: ${{ secrets.HF }}
|
| 50 |
+
run: git push --force https://cccmatthew:$HF@huggingface.co/spaces/cccmatthew/gp30-dev main
|
| 51 |
+
|
| 52 |
+
DeployProd:
|
| 53 |
+
name: Deploy to Production
|
| 54 |
+
needs: [DeployStaging]
|
| 55 |
+
runs-on: ubuntu-latest
|
| 56 |
+
environment:
|
| 57 |
+
name: Prod
|
| 58 |
+
url: 'https://huggingface.co/spaces/cccmatthew/gp30-deployment'
|
| 59 |
+
steps:
|
| 60 |
- name: Add remote
|
| 61 |
env:
|
| 62 |
HF: ${{ secrets.HF }}
|
| 63 |
run: git remote add space https://cccmatthew:$HF@huggingface.co/spaces/cccmatthew/gp30-deployment
|
| 64 |
+
- name: Deploy
|
| 65 |
env:
|
| 66 |
HF: ${{ secrets.HF }}
|
| 67 |
run: git push --force https://cccmatthew:$HF@huggingface.co/spaces/cccmatthew/gp30-deployment main
|
inference.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import csv
|
| 4 |
+
import datetime
|
| 5 |
+
import time
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
##########################
|
| 11 |
+
SECRET = os.environ["api_secret"]
|
| 12 |
+
headers = {"Authorization": "Bearer " + SECRET}
|
| 13 |
+
API_URL = "https://api-inference.huggingface.co/models/cccmatthew/surrey-gp30"
|
| 14 |
+
##########################
|
| 15 |
+
|
| 16 |
+
def log_to_csv(sentence, results, response_time):
|
| 17 |
+
with open('model_interactions.csv', 'a', newline='') as file:
|
| 18 |
+
writer = csv.writer(file)
|
| 19 |
+
writer.writerow([datetime.datetime.now(), sentence, results, response_time])
|
| 20 |
+
|
| 21 |
+
def send_request_with_retry(url, headers, json_data, retries=3, backoff_factor=1):
|
| 22 |
+
"""Send request with retries on timeouts and HTTP 503 errors."""
|
| 23 |
+
for attempt in range(retries):
|
| 24 |
+
start_time = time.time()
|
| 25 |
+
try:
|
| 26 |
+
response = requests.post(url, headers=headers, json=json_data)
|
| 27 |
+
response.raise_for_status()
|
| 28 |
+
response_time = time.time() - start_time
|
| 29 |
+
return response, response_time
|
| 30 |
+
except requests.exceptions.HTTPError as e:
|
| 31 |
+
if response.status_code == 503:
|
| 32 |
+
st.info('Server is unavailable, retrying...')
|
| 33 |
+
else:
|
| 34 |
+
raise
|
| 35 |
+
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
|
| 36 |
+
st.info(f"Network issue ({str(e)}), retrying...")
|
| 37 |
+
time.sleep(backoff_factor * (2 ** attempt))
|
| 38 |
+
|
| 39 |
+
st.error("Failed to process request after several attempts.")
|
| 40 |
+
return None, None
|
| 41 |
+
|
| 42 |
+
sentence = [
|
| 43 |
+
"RAFs are plotted for a selection of neurons in the dorsal zone (DZ) of auditory cortex in Figure 1.",
|
| 44 |
+
"Light dissolved inorganic carbon (DIC) resulting from the oxidation of hydrocarbons.",
|
| 45 |
+
"Images were acquired using a GE 3.0T MRI scanner with an upgrade for echo-planar imaging (EPI)."
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
API_URL = API_URL
|
| 49 |
+
headers = headers
|
| 50 |
+
response, response_time = send_request_with_retry(API_URL, headers, {"inputs": sentence})
|
| 51 |
+
|
| 52 |
+
if response is not None:
|
| 53 |
+
results = response.json()
|
| 54 |
+
print(results)
|
| 55 |
+
st.write('Results:')
|
| 56 |
+
log_to_csv(sentence, results, response_time)
|
| 57 |
+
|
| 58 |
+
else:
|
| 59 |
+
st.error("Unable to classify the sentence due to server issues.")
|