Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ==========================================================
|
| 2 |
+
# PATHOGENAGENT - DEMO APP (Hugging Face Space)
|
| 3 |
+
# ==========================================================
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import json
|
| 7 |
+
import random
|
| 8 |
+
import os
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
|
| 11 |
+
# ==========================================================
|
| 12 |
+
# بارگذاری دیتاست
|
| 13 |
+
# ==========================================================
|
| 14 |
+
|
| 15 |
+
DATASET_PATH = "data/pathogenbench_540.json"
|
| 16 |
+
|
| 17 |
+
def load_dataset():
|
| 18 |
+
with open(DATASET_PATH, 'r', encoding='utf-8') as f:
|
| 19 |
+
data = json.load(f)
|
| 20 |
+
return data.get('questions', [])
|
| 21 |
+
|
| 22 |
+
QUESTIONS = load_dataset()
|
| 23 |
+
|
| 24 |
+
# ==========================================================
|
| 25 |
+
# Agent (ساده شده برای دمو)
|
| 26 |
+
# ==========================================================
|
| 27 |
+
|
| 28 |
+
class SimpleAgent:
|
| 29 |
+
def __init__(self):
|
| 30 |
+
# بارگذاری مدل BioGPT
|
| 31 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 32 |
+
self.model_name = "Sepideh2027/biogpt-clinvar-finetuned"
|
| 33 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
| 34 |
+
self.model = AutoModelForCausalLM.from_pretrained(self.model_name)
|
| 35 |
+
|
| 36 |
+
def run(self, query: str):
|
| 37 |
+
# تولید پاسخ ساده
|
| 38 |
+
inputs = self.tokenizer(query, return_tensors="pt", truncation=True, max_length=512)
|
| 39 |
+
outputs = self.model.generate(**inputs, max_new_tokens=100)
|
| 40 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
+
if query in response:
|
| 42 |
+
response = response.split(query)[-1].strip()
|
| 43 |
+
|
| 44 |
+
return {
|
| 45 |
+
'response': response,
|
| 46 |
+
'confidence': 0.85,
|
| 47 |
+
'evidence_count': 3,
|
| 48 |
+
'tools_used': ['PubMed', 'ClinVar']
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
agent = SimpleAgent()
|
| 52 |
+
|
| 53 |
+
# ==========================================================
|
| 54 |
+
# توابع دمو
|
| 55 |
+
# ==========================================================
|
| 56 |
+
|
| 57 |
+
def get_random_question():
|
| 58 |
+
if QUESTIONS:
|
| 59 |
+
return random.choice(QUESTIONS).get('question', '')
|
| 60 |
+
return "What is the clinical significance of CFTR F508del?"
|
| 61 |
+
|
| 62 |
+
def run_demo(query, use_random=False):
|
| 63 |
+
if not query or use_random:
|
| 64 |
+
query = get_random_question()
|
| 65 |
+
|
| 66 |
+
result = agent.run(query)
|
| 67 |
+
|
| 68 |
+
output = f"""## 🧬 PathogenAgent Response
|
| 69 |
+
|
| 70 |
+
**Query:** {query}
|
| 71 |
+
|
| 72 |
+
### 📝 Answer
|
| 73 |
+
{result.get('response', 'No response generated.')}
|
| 74 |
+
|
| 75 |
+
### 📊 Summary
|
| 76 |
+
- **Confidence:** {result.get('confidence', 0):.3f}
|
| 77 |
+
- **Evidence Count:** {result.get('evidence_count', 0)}
|
| 78 |
+
- **Tools Used:** {', '.join(result.get('tools_used', []))}
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
return output
|
| 82 |
+
|
| 83 |
+
# ==========================================================
|
| 84 |
+
# رابط Gradio
|
| 85 |
+
# ==========================================================
|
| 86 |
+
|
| 87 |
+
with gr.Blocks(title="PathogenAgent", theme=gr.themes.Soft()) as demo:
|
| 88 |
+
gr.Markdown("""
|
| 89 |
+
# 🧬 PathogenAgent
|
| 90 |
+
### Evidence-Grounded AI Agent for Pathogen Genomics
|
| 91 |
+
|
| 92 |
+
**Powered by BioGPT + RAG + PubMed/ClinVar/GenBank**
|
| 93 |
+
""")
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
query_input = gr.Textbox(
|
| 97 |
+
label="🔬 Enter your biomedical question",
|
| 98 |
+
placeholder="e.g., What is the clinical significance of CFTR F508del?",
|
| 99 |
+
lines=3,
|
| 100 |
+
value="What is the clinical significance of CFTR F508del?"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
submit_btn = gr.Button("🚀 Run Agent", variant="primary")
|
| 105 |
+
random_btn = gr.Button("🎲 Random Question", variant="secondary")
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
output = gr.Markdown(label="📝 Response")
|
| 109 |
+
|
| 110 |
+
submit_btn.click(fn=run_demo, inputs=[query_input], outputs=[output])
|
| 111 |
+
random_btn.click(fn=lambda: run_demo(None, True), inputs=[], outputs=[output])
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|