babilonczyk commited on
Commit
2ccdeda
·
verified ·
1 Parent(s): 2631360

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -1,23 +1,33 @@
1
  import gradio as gr
2
- import zipfile
3
  import sqlite3
4
  import os
 
5
 
6
- def get_connection_from_zip(zip_path: str, db_filename: str) -> sqlite3.Connection:
7
- # Extract DB file from ZIP to disk as "temp.db"
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
- # Open SQLite connection (read-only)
14
- return sqlite3.connect("temp.db", check_same_thread=False)
 
 
 
 
 
 
 
 
 
15
 
16
- # Set up DB
17
- conn = get_connection_from_zip("proteins.db.zip", "proteins.db")
 
 
 
 
 
18
  cursor = conn.cursor()
19
 
20
- # Query logic
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,))