Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import zipfile
|
| 3 |
import sqlite3
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
with zipfile.ZipFile(zip_path) as z:
|
| 9 |
-
with z.open(db_filename) as f:
|
| 10 |
-
with open("temp.db", "wb") as out_file:
|
| 11 |
-
out_file.write(f.read())
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
cursor = conn.cursor()
|
| 19 |
|
| 20 |
-
# Query
|
| 21 |
def search_by_sequence(seq: str) -> str:
|
| 22 |
seq = seq.strip().upper()
|
| 23 |
cursor.execute("SELECT * FROM proteins WHERE sequence = ?", (seq,))
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import sqlite3
|
| 3 |
import os
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
+
HF_DB_URL = "https://huggingface.co/datasets/babilonczyk/swiss_prot/resolve/main/proteins.db"
|
| 7 |
+
LOCAL_DB_PATH = "proteins.db"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Download the DB only if not already cached
|
| 10 |
+
def download_db(url: str, local_path: str):
|
| 11 |
+
if not os.path.exists(local_path):
|
| 12 |
+
print(f"⬇️ Downloading database from {url}...")
|
| 13 |
+
r = requests.get(url)
|
| 14 |
+
r.raise_for_status()
|
| 15 |
+
with open(local_path, "wb") as f:
|
| 16 |
+
f.write(r.content)
|
| 17 |
+
print("✅ Download complete.")
|
| 18 |
+
else:
|
| 19 |
+
print("🟢 Database already exists locally.")
|
| 20 |
|
| 21 |
+
# Connect to SQLite
|
| 22 |
+
def get_connection(db_path: str) -> sqlite3.Connection:
|
| 23 |
+
return sqlite3.connect(db_path, check_same_thread=False)
|
| 24 |
+
|
| 25 |
+
# Setup
|
| 26 |
+
download_db(HF_DB_URL, LOCAL_DB_PATH)
|
| 27 |
+
conn = get_connection(LOCAL_DB_PATH)
|
| 28 |
cursor = conn.cursor()
|
| 29 |
|
| 30 |
+
# Query function
|
| 31 |
def search_by_sequence(seq: str) -> str:
|
| 32 |
seq = seq.strip().upper()
|
| 33 |
cursor.execute("SELECT * FROM proteins WHERE sequence = ?", (seq,))
|