ZDPLI commited on
Commit
6336417
Β·
verified Β·
1 Parent(s): a014337

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import logging
3
+ import gradio as gr
4
+ from PIL import Image
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
+ from llama_cpp import Llama
7
+ from huggingface_hub import hf_hub_download
8
+
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
+
12
+ # ------------------------------
13
+ # πŸ”Ή Load Bioformer-8L Model
14
+ # ------------------------------
15
+ BIOFORMER_MODEL = "bioformers/bioformer-8L"
16
+ bioformer_tokenizer = AutoTokenizer.from_pretrained(BIOFORMER_MODEL)
17
+ bioformer_model = AutoModelForCausalLM.from_pretrained(BIOFORMER_MODEL)
18
+
19
+ # ------------------------------
20
+ # πŸ”Ή Load DeepSeek-R1-Distill-Qwen-7B-GGUF Model
21
+ # ------------------------------
22
+ DEEPSEEK_REPO = "lmstudio-community/DeepSeek-R1-Distill-Qwen-7B-GGUF"
23
+ DEEPSEEK_FILENAME = "DeepSeek-R1-Distill-Qwen-7B-Q4_0.gguf"
24
+
25
+ model_path = hf_hub_download(repo_id=DEEPSEEK_REPO, filename=DEEPSEEK_FILENAME)
26
+
27
+ llm = Llama(
28
+ model_path=model_path,
29
+ n_ctx=4096,
30
+ n_gpu_layers=0, # CPU inference
31
+ logits_all=True,
32
+ n_batch=256
33
+ )
34
+
35
+ logger.info("Models Loaded Successfully.")
36
+
37
+ # ------------------------------
38
+ # πŸ”Ή Unified Medical Prompt
39
+ # ------------------------------
40
+ UNIFIED_MEDICAL_PROMPT = """
41
+ You are an advanced Medical AI Assistant capable of providing thorough,
42
+ comprehensive answers for a wide range of medical specialties:
43
+ General Practice, Radiology, Cardiology, Neurology, Psychiatry, Pediatrics,
44
+ Endocrinology, Oncology, and more.
45
+
46
+ You can:
47
+ 1) Analyze images if provided (Radiology).
48
+ 2) Retrieve relevant documents from a knowledge base (Vector Store).
49
+ 3) Provide scientific, evidence-based explanations and references when possible.
50
+
51
+ Always strive to provide a detailed, helpful, and empathetic response.
52
+ """
53
+
54
+ # ------------------------------
55
+ # πŸ”Ή Chat Function
56
+ # ------------------------------
57
+ def chat_with_agent(user_query, image_file=None):
58
+ # Combine context
59
+ combined_context = f"""
60
+ {UNIFIED_MEDICAL_PROMPT}
61
+
62
+ Patient Query: "{user_query}"
63
+ Your Response:
64
+ """
65
+
66
+ # Generate response using DeepSeek-R1-Distill model
67
+ response_accumulator = ""
68
+ for token in llm(
69
+ prompt=combined_context,
70
+ max_tokens=1024,
71
+ temperature=0.7,
72
+ top_p=0.9,
73
+ stream=True
74
+ ):
75
+ partial_text = token["choices"][0]["text"]
76
+ response_accumulator += partial_text
77
+ yield response_accumulator
78
+
79
+ # ------------------------------
80
+ # πŸ”Ή Gradio Interface
81
+ # ------------------------------
82
+ with gr.Blocks(title="πŸ₯ Llama3-Med AI Assistant") as demo:
83
+ gr.Markdown("""
84
+ # πŸ₯ Llama3-Med AI Assistant
85
+ _Your intelligent medical assistant powered by advanced AI._
86
+ """)
87
+
88
+ with gr.Row():
89
+ user_input = gr.Textbox(label="πŸ’¬ Ask a medical question", placeholder="Type your question here...")
90
+ image_file = gr.Image(label="πŸ“· Upload Medical Image (Optional)", type="filepath")
91
+
92
+ submit_btn = gr.Button("πŸš€ Submit", variant="primary")
93
+ output_text = gr.Textbox(label="πŸ“ Assistant's Response", interactive=False, lines=25)
94
+
95
+ submit_btn.click(fn=chat_with_agent, inputs=[user_input, image_file], outputs=output_text)
96
+
97
+ if __name__ == "__main__":
98
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860)