babilonczyk commited on
Commit
a86d83f
·
verified ·
1 Parent(s): b1a1cbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -31
app.py CHANGED
@@ -28,40 +28,26 @@ download_db(HF_DB_URL, LOCAL_DB_PATH)
28
  conn = get_connection(LOCAL_DB_PATH)
29
  cursor = conn.cursor()
30
 
31
-
32
- def print_schema():
33
- cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
34
- tables = cursor.fetchall()
35
- print("📋 Tables:", tables)
36
-
37
- for (table_name,) in tables:
38
- print(f"\n🔎 Columns in table '{table_name}':")
39
- cursor.execute(f"PRAGMA table_info({table_name});")
40
- for col in cursor.fetchall():
41
- print(col)
42
-
43
- print_schema()
44
-
45
- # Query function
46
- def search_by_sequence(seq: str) -> str:
47
- seq = seq.strip().upper()
48
- cursor.execute("SELECT json FROM proteins")
49
- for (json_blob,) in cursor.fetchall():
50
- try:
51
- data = json.loads(json_blob)
52
- if data.get("sequence", "").upper() == seq:
53
- return json.dumps(data, indent=2)
54
- except json.JSONDecodeError:
55
- continue
56
- return "No match found."
57
 
58
  # Gradio app
59
  demo = gr.Interface(
60
- fn=search_by_sequence,
61
- inputs=gr.Textbox(label="Enter protein sequence"),
62
- outputs=gr.Textbox(label="Protein metadata"),
63
- title="SwissProt Search",
64
- description="Search Swiss-Prot by full protein sequence (exact match)"
65
  )
66
 
67
  demo.launch(show_api=True)
 
28
  conn = get_connection(LOCAL_DB_PATH)
29
  cursor = conn.cursor()
30
 
31
+ # Search by protein ID (exact match)
32
+ def search_by_id(protein_id: str) -> str:
33
+ protein_id = protein_id.strip().upper()
34
+ cursor.execute("SELECT json FROM proteins WHERE id = ?", (protein_id,))
35
+ row = cursor.fetchone()
36
+ if not row:
37
+ return "❌ No match found."
38
+ try:
39
+ data = json.loads(row[0])
40
+ return json.dumps(data, indent=2)
41
+ except json.JSONDecodeError:
42
+ return "❌ Found row but failed to parse JSON."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # Gradio app
45
  demo = gr.Interface(
46
+ fn=search_by_id,
47
+ inputs=gr.Textbox(label="Enter UniProt ID (e.g. P0DTC2)"),
48
+ outputs=gr.Textbox(label="Protein metadata (JSON)"),
49
+ title="SwissProt Search by ID",
50
+ description="Search Swiss-Prot database by UniProt protein ID"
51
  )
52
 
53
  demo.launch(show_api=True)