babilonczyk commited on
Commit
baa1916
·
verified ·
1 Parent(s): cf27fed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -1,37 +1,32 @@
1
  import gradio as gr
2
  import zipfile
3
  import sqlite3
4
- import tempfile
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, 'r') as zip_ref:
11
- with zip_ref.open(db_filename) as db_file:
12
- with tempfile.NamedTemporaryFile(delete=False, suffix=".db") as tmp_file:
13
- shutil.copyfileobj(db_file, tmp_file)
14
- tmp_db_path = tmp_file.name
 
 
 
 
 
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 protein found for the given sequence."
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)