Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import zipfile
|
| 3 |
import sqlite3
|
| 4 |
-
import
|
| 5 |
-
import shutil
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
# Extract DB from ZIP to a temporary file
|
| 9 |
def get_connection_from_zip(zip_path: str, db_filename: str) -> sqlite3.Connection:
|
| 10 |
-
with zipfile.ZipFile(zip_path
|
| 11 |
-
with
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
return sqlite3.connect(tmp_db_path)
|
| 17 |
-
|
| 18 |
-
# Open connection at startup
|
| 19 |
conn = get_connection_from_zip("proteins.db.zip", "proteins.db")
|
| 20 |
cursor = conn.cursor()
|
| 21 |
|
| 22 |
-
# Query by protein sequence
|
| 23 |
def search_by_sequence(seq: str) -> str:
|
| 24 |
seq = seq.strip().upper()
|
| 25 |
cursor.execute("SELECT * FROM proteins WHERE sequence = ?", (seq,))
|
| 26 |
row = cursor.fetchone()
|
| 27 |
-
|
| 28 |
if not row:
|
| 29 |
-
return "No
|
| 30 |
-
|
| 31 |
col_names = [desc[0] for desc in cursor.description]
|
| 32 |
return "\n".join(f"{k}: {v}" for k, v in zip(col_names, row))
|
| 33 |
|
| 34 |
-
# Gradio UI
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=search_by_sequence,
|
| 37 |
inputs=gr.Textbox(label="Enter protein sequence"),
|
|
@@ -40,4 +35,4 @@ demo = gr.Interface(
|
|
| 40 |
description="Search Swiss-Prot by full protein sequence (exact match)"
|
| 41 |
)
|
| 42 |
|
| 43 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import zipfile
|
| 3 |
import sqlite3
|
| 4 |
+
import io
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
def get_connection_from_zip(zip_path: str, db_filename: str) -> sqlite3.Connection:
|
| 7 |
+
with zipfile.ZipFile(zip_path) as z:
|
| 8 |
+
with z.open(db_filename) as f:
|
| 9 |
+
db_bytes = io.BytesIO(f.read())
|
| 10 |
+
conn = sqlite3.connect("file:memdb1?mode=memory&cache=shared", uri=True, check_same_thread=False)
|
| 11 |
+
disk_conn = sqlite3.connect(f"file:temp.db?mode=ro", uri=True)
|
| 12 |
+
with open("temp.db", "wb") as temp_file:
|
| 13 |
+
temp_file.write(db_bytes.getvalue())
|
| 14 |
+
disk_conn = sqlite3.connect("temp.db", check_same_thread=False)
|
| 15 |
+
disk_conn.backup(conn)
|
| 16 |
+
return conn
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
conn = get_connection_from_zip("proteins.db.zip", "proteins.db")
|
| 19 |
cursor = conn.cursor()
|
| 20 |
|
|
|
|
| 21 |
def search_by_sequence(seq: str) -> str:
|
| 22 |
seq = seq.strip().upper()
|
| 23 |
cursor.execute("SELECT * FROM proteins WHERE sequence = ?", (seq,))
|
| 24 |
row = cursor.fetchone()
|
|
|
|
| 25 |
if not row:
|
| 26 |
+
return "No match found."
|
|
|
|
| 27 |
col_names = [desc[0] for desc in cursor.description]
|
| 28 |
return "\n".join(f"{k}: {v}" for k, v in zip(col_names, row))
|
| 29 |
|
|
|
|
| 30 |
demo = gr.Interface(
|
| 31 |
fn=search_by_sequence,
|
| 32 |
inputs=gr.Textbox(label="Enter protein sequence"),
|
|
|
|
| 35 |
description="Search Swiss-Prot by full protein sequence (exact match)"
|
| 36 |
)
|
| 37 |
|
| 38 |
+
demo.launch(show_api=True, ssr=False)
|