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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -1,23 +1,18 @@
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,))
@@ -27,6 +22,7 @@ def search_by_sequence(seq: str) -> str:
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"),
 
 
 
 
 
 
1
  def get_connection_from_zip(zip_path: str, db_filename: str) -> sqlite3.Connection:
2
+ # Extract DB file from ZIP to disk as "temp.db"
3
  with zipfile.ZipFile(zip_path) as z:
4
  with z.open(db_filename) as f:
5
+ with open("temp.db", "wb") as out_file:
6
+ out_file.write(f.read())
7
+
8
+ # Open SQLite connection (read-only)
9
+ return sqlite3.connect("temp.db", check_same_thread=False)
 
 
 
10
 
11
+ # Set up DB
12
  conn = get_connection_from_zip("proteins.db.zip", "proteins.db")
13
  cursor = conn.cursor()
14
 
15
+ # Query logic
16
  def search_by_sequence(seq: str) -> str:
17
  seq = seq.strip().upper()
18
  cursor.execute("SELECT * FROM proteins WHERE sequence = ?", (seq,))
 
22
  col_names = [desc[0] for desc in cursor.description]
23
  return "\n".join(f"{k}: {v}" for k, v in zip(col_names, row))
24
 
25
+ # Gradio app
26
  demo = gr.Interface(
27
  fn=search_by_sequence,
28
  inputs=gr.Textbox(label="Enter protein sequence"),