iprashantsmp commited on
Commit
6462db2
·
1 Parent(s): 10bd5e4

using the transformers

Browse files
Files changed (4) hide show
  1. Dockerfile +0 -18
  2. README.md +168 -1
  3. app.py +85 -38
  4. requirements.txt +5 -3
Dockerfile DELETED
@@ -1,18 +0,0 @@
1
- FROM python:3.10-slim
2
-
3
- WORKDIR /app
4
-
5
- # Install only what you actually need
6
- RUN apt-get update && \
7
- apt-get install -y libopenblas-dev && \
8
- rm -rf /var/lib/apt/lists/*
9
-
10
- COPY requirements.txt .
11
- RUN pip install --upgrade pip
12
- # This will pull the prebuilt CPU wheels
13
- RUN pip install --no-cache-dir -r requirements.txt
14
-
15
- COPY . .
16
-
17
- EXPOSE 7860
18
- CMD ["python", "app.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -7,7 +7,174 @@ sdk: gradio
7
  sdk_version: 5.39.0
8
  app_file: app.py
9
  pinned: false
10
- hardware: cpu
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  sdk_version: 5.39.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+
15
+ # 🦷 Dental AI Assistant
16
+
17
+ An advanced dental consultation and medication extraction system powered by AI. This application provides dental advice, medication recommendations, and intelligent text analysis for medical documents.
18
+
19
+ ## ✨ Features
20
+
21
+ - **🩺 Dental Consultation**: Get AI-powered dental advice with detailed medication regimens
22
+ - **💊 Medication Extraction**: Extract and highlight medications from medical text using NLP
23
+ - **🎨 Interactive Visualization**: Visual representation of extracted medication entities
24
+ - **⚡ Quick Questions**: Pre-built common dental questions for instant answers
25
+ - **⚙️ Customizable Settings**: Adjust response length and creativity parameters
26
+ - **🚀 GPU/CPU Support**: Automatic device detection and optimization
27
+ - **📱 Modern UI**: Clean, responsive Gradio interface
28
+
29
+ ## 🛠️ Installation
30
+
31
+ ### Prerequisites
32
+
33
+ - Python 3.8+
34
+ - CUDA-compatible GPU (optional, for faster inference)
35
+
36
+ ### Step 1: Clone the Repository
37
+
38
+ ```bash
39
+ git clone https://huggingface.co/spaces/iprashantsmp/Dental_AI_Assistant/
40
+ cd Dental_AI_Assistant
41
+ ```
42
+
43
+ ### Step 2: Install Dependencies
44
+
45
+ ```bash
46
+ pip install -r requirements.txt
47
+ ```
48
+
49
+
50
+ ### Step 3: Get API Keys
51
+
52
+ **Gemini API Key** (required for medication extraction):
53
+ 1. Go to [Google AI Studio](https://aistudio.google.com)
54
+ 2. Click 'Get API Key'
55
+ 3. Create a new API key
56
+ 4. Keep it secure for use in the application
57
+
58
+ ## 🚀 Usage
59
+
60
+ ### Running the Application
61
+
62
+ ```bash
63
+ gradio app.py
64
+ ```
65
+
66
+ The application will start on `http://127.0.0.1:7860`
67
+
68
+ ### Features Overview
69
+
70
+ #### 1. Dental Consultation Tab
71
+ - Ask dental questions and receive AI-powered advice
72
+ - Get detailed 3-day medication regimens
73
+ - Use quick questions for common dental issues
74
+ - Adjust response parameters (max tokens, temperature)
75
+
76
+ #### 2. Medication Extraction Tab
77
+ - Paste medical text to extract medication information
78
+ - Get highlighted text with identified entities
79
+ - View interactive visualizations of extracted data
80
+ - Export results for further analysis
81
+
82
+ #### 3. Help & Setup Tab
83
+ - Complete setup instructions
84
+ - API key configuration guide
85
+ - Feature documentation
86
+
87
+ ## 🔧 Configuration
88
+
89
+ ### Model Settings
90
+
91
+ The application uses the `yasserrmd/DentaInstruct-1.2B` model from Hugging Face:
92
+
93
+ - **Model Type**: Causal Language Model
94
+ - **Framework**: Transformers
95
+ - **Device**: Auto-detected (GPU/CPU)
96
+ - **Precision**: Float16 (GPU) / Float32 (CPU)
97
+
98
+ ### Generation Parameters
99
+
100
+ - **Max Tokens**: 500-4000 (default: 2048)
101
+ - **Temperature**: 0.1-1.0 (default: 0.7)
102
+ - **Top-p**: 0.9 (fixed)
103
+ - **Do Sample**: True
104
+
105
+ ## 📁 Project Structure
106
+
107
+ ```
108
+ dental-ai-assistant/
109
+ ├── app.py # Main application file
110
+ ├── requirements.txt # Python dependencies
111
+ ├── README.md # This file
112
+ ```
113
+
114
+ ## 🔑 API Keys Setup
115
+
116
+ ### Gemini API Key
117
+
118
+ 1. Visit [Google AI Studio](https://aistudio.google.com)
119
+ 2. Sign in with your Google account
120
+ 3. Navigate to "Get API Key"
121
+ 4. Create a new project or select existing
122
+ 5. Generate API key
123
+ 6. Copy and use in the application
124
+
125
+ **Note**: Keep your API keys secure and never commit them to version control.
126
+
127
+ ## 🎯 Quick Start Examples
128
+
129
+ ### Example 1: Dental Consultation
130
+ ```
131
+ Question: "I have a severe toothache with swelling, provide 3-day medication"
132
+
133
+ Expected Response: Detailed medication regimen including:
134
+ - Antibiotics (dosage, frequency, duration)
135
+ - Pain relievers (mechanism of action)
136
+ - Anti-inflammatory medications
137
+ - Professional consultation disclaimer
138
+ ```
139
+
140
+ ### Example 2: Medication Extraction
141
+ ```
142
+ Input Text: "Patient prescribed 500mg Amoxicillin TID for 7 days and 400mg Ibuprofen QID PRN for pain"
143
+
144
+ Expected Output:
145
+ - Medication: Amoxicillin, Ibuprofen
146
+ - Dosage: 500mg, 400mg
147
+ - Frequency: TID, QID PRN
148
+ - Duration: 7 days, as needed
149
+ ```
150
+
151
+ ## 🚨 Important Disclaimers
152
+
153
+ ⚠️ **Medical Disclaimer**: This AI assistant is for educational purposes only. Always consult with a qualified dentist or healthcare professional for medical advice, diagnosis, or treatment.
154
+
155
+ ⚠️ **Accuracy**: While the AI strives for accuracy, medical information should always be verified with healthcare professionals.
156
+
157
+ ⚠️ **Emergency**: For dental emergencies, contact your dentist or emergency services immediately.
158
+
159
+
160
+ ### Performance Optimization
161
+
162
+ - **For faster inference**: Use GPU with CUDA
163
+ - **For lower memory usage**: Reduce max_tokens and batch size
164
+ - **For better accuracy**: Increase temperature for more creative responses
165
+
166
+
167
+ ## 📄 License
168
+
169
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
170
+
171
+ ## 🙏 Acknowledgments
172
+
173
+ - **Model**: [yasserrmd/DentaInstruct-1.2B](https://huggingface.co/yasserrmd/DentaInstruct-1.2B)
174
+ - **Framework**: [Hugging Face Transformers](https://huggingface.co/transformers/)
175
+ - **UI**: [Gradio](https://gradio.app/)
176
+ - **NLP**: [LangExtract](https://github.com/google/langextract)
177
+ - **API**: [Google Gemini](https://ai.google.dev/)
178
+
179
+
180
+ **Built with ❤️ for the dental community**
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from llama_cpp import Llama
3
  import langextract as lx
4
  import json
5
  import re
@@ -10,44 +10,51 @@ import time
10
  import os
11
  from pathlib import Path
12
  import tempfile
 
13
 
14
- # Global variable to store the loaded model and token
15
  dental_model = None
 
16
  current_token = None
17
  output_directory = Path(".")
18
 
19
- def load_dental_gguf_model():
20
- """Load the dental GGUF model"""
21
- global dental_model
22
- if dental_model is None:
23
  try:
24
- print("Loading GGUF model... This may take a moment on first run.")
25
- dental_model = Llama.from_pretrained(
26
- repo_id="yasserrmd/DentaInstruct-1.2B-gguf",
27
- filename="DentaInstruct-1.2B-Q4_K_M.gguf",
28
- verbose=False,
29
- n_ctx=4096,
30
- n_threads=4,
 
31
  )
 
 
 
 
 
32
  print("Model loaded successfully!")
33
- return dental_model
34
  except Exception as e:
35
- print(f"Error loading GGUF model: {str(e)}")
36
- return None
37
- return dental_model
38
 
39
  def generate_dental_response(
40
  question: str,
41
  max_tokens: int = 2048,
42
  temperature: float = 0.7
43
  ) -> str:
44
- """Generate response using GGUF model"""
45
-
46
 
47
- # Load model with the provided token
48
- llm = load_dental_gguf_model()
49
- if not llm:
50
- return "❌ GGUF model not available."
51
 
52
  try:
53
  system_prompt = """You are a dental AI assistant. When providing medication recommendations, you must:
@@ -61,17 +68,57 @@ def generate_dental_response(
61
  {"role": "user", "content": question}
62
  ]
63
 
64
- response = llm.create_chat_completion(
65
- messages=messages,
66
- max_tokens=max_tokens,
67
- temperature=temperature,
68
- top_p=0.9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  )
70
 
71
- return response['choices'][0]['message']['content'].strip()
72
 
73
  except Exception as e:
74
- return f"❌ Error generating response with GGUF model: {str(e)}"
75
 
76
  def extract_medications(text: str, gemini_api_key: str = "") -> Tuple[str, str, str]:
77
  """Extract medication information from text"""
@@ -290,9 +337,9 @@ def create_gradio_interface():
290
 
291
  gr.Markdown("""
292
  **Model Info:**
293
- - Using GGUF Model
294
- - Optimized for performance
295
- - Requires HF token for access
296
  """)
297
 
298
  response_output = gr.Textbox(
@@ -372,7 +419,7 @@ def create_gradio_interface():
372
  ## 🚀 Getting Started
373
 
374
  ### Model:
375
- **GGUF Model**: Faster, more efficient, works offline after download
376
 
377
  ### 🔑 API Key Setup:
378
 
@@ -383,7 +430,7 @@ def create_gradio_interface():
383
 
384
  ### 📦 Installation Requirements:
385
  ```bash
386
- pip install gradio llama-cpp-python langextract pandas requests
387
  ```
388
 
389
  ### 🩺 Features:
@@ -392,6 +439,7 @@ def create_gradio_interface():
392
  - **Interactive Visualization**: Visual representation of extracted medication entities
393
  - **Quick Questions**: Pre-built common dental questions
394
  - **Customizable Settings**: Adjust response length and creativity
 
395
 
396
  ### ⚠️ Important Disclaimer:
397
  This AI assistant is for educational purposes only. Always consult with a qualified dentist for professional medical advice.
@@ -403,7 +451,7 @@ def create_gradio_interface():
403
  <p><strong>⚠️ Disclaimer:</strong> This AI assistant is for educational purposes only.
404
  Always consult with a qualified dentist for professional medical advice.</p>
405
  <p style="text-align: center; margin-top: 1rem;">
406
- 🦷 Built with Gradio | Powered by DentaInstruct-1.2B
407
  </p>
408
  </div>
409
  """)
@@ -416,6 +464,5 @@ if __name__ == "__main__":
416
  demo.queue()
417
  demo.launch(
418
  share=False,
419
- show_error=True,
420
- enable_queue=True
421
  )
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import langextract as lx
4
  import json
5
  import re
 
10
  import os
11
  from pathlib import Path
12
  import tempfile
13
+ import torch
14
 
15
+ # Global variables to store the loaded model and tokenizer
16
  dental_model = None
17
+ dental_tokenizer = None
18
  current_token = None
19
  output_directory = Path(".")
20
 
21
+ def load_dental_transformers_model():
22
+ """Load the dental model using transformers"""
23
+ global dental_model, dental_tokenizer
24
+ if dental_model is None or dental_tokenizer is None:
25
  try:
26
+ print("Loading transformers model... This may take a moment on first run.")
27
+
28
+ # Load tokenizer and model
29
+ dental_tokenizer = AutoTokenizer.from_pretrained("yasserrmd/DentaInstruct-1.2B")
30
+ dental_model = AutoModelForCausalLM.from_pretrained(
31
+ "yasserrmd/DentaInstruct-1.2B",
32
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
33
+ device_map="auto" if torch.cuda.is_available() else None
34
  )
35
+
36
+ # Set pad token if not set
37
+ if dental_tokenizer.pad_token is None:
38
+ dental_tokenizer.pad_token = dental_tokenizer.eos_token
39
+
40
  print("Model loaded successfully!")
41
+ return dental_model, dental_tokenizer
42
  except Exception as e:
43
+ print(f"Error loading transformers model: {str(e)}")
44
+ return None, None
45
+ return dental_model, dental_tokenizer
46
 
47
  def generate_dental_response(
48
  question: str,
49
  max_tokens: int = 2048,
50
  temperature: float = 0.7
51
  ) -> str:
52
+ """Generate response using transformers model"""
 
53
 
54
+ # Load model and tokenizer
55
+ model, tokenizer = load_dental_transformers_model()
56
+ if not model or not tokenizer:
57
+ return "❌ Transformers model not available."
58
 
59
  try:
60
  system_prompt = """You are a dental AI assistant. When providing medication recommendations, you must:
 
68
  {"role": "user", "content": question}
69
  ]
70
 
71
+ # Apply chat template
72
+ try:
73
+ # Try with chat template first
74
+ input_text = tokenizer.apply_chat_template(
75
+ messages,
76
+ add_generation_prompt=True,
77
+ tokenize=False
78
+ )
79
+ except:
80
+ # Fallback to simple concatenation if chat template fails
81
+ input_text = f"{system_prompt}\n\nUser: {question}\n\nAssistant:"
82
+
83
+ # Tokenize the input
84
+ inputs = tokenizer(
85
+ input_text,
86
+ return_tensors="pt",
87
+ padding=True,
88
+ truncation=True,
89
+ max_length=2048
90
+ )
91
+
92
+ # Remove token_type_ids if present (not needed for most models)
93
+ if 'token_type_ids' in inputs:
94
+ del inputs['token_type_ids']
95
+
96
+ # Move to device
97
+ inputs = {k: v.to(model.device) for k, v in inputs.items()}
98
+
99
+ # Generate response
100
+ with torch.no_grad():
101
+ outputs = model.generate(
102
+ input_ids=inputs['input_ids'],
103
+ attention_mask=inputs['attention_mask'],
104
+ max_new_tokens=max_tokens,
105
+ temperature=temperature,
106
+ top_p=0.9,
107
+ do_sample=True,
108
+ pad_token_id=tokenizer.eos_token_id,
109
+ eos_token_id=tokenizer.eos_token_id
110
+ )
111
+
112
+ # Decode only the new tokens (response)
113
+ response = tokenizer.decode(
114
+ outputs[0][inputs['input_ids'].shape[-1]:],
115
+ skip_special_tokens=True
116
  )
117
 
118
+ return response.strip()
119
 
120
  except Exception as e:
121
+ return f"❌ Error generating response with transformers model: {str(e)}"
122
 
123
  def extract_medications(text: str, gemini_api_key: str = "") -> Tuple[str, str, str]:
124
  """Extract medication information from text"""
 
337
 
338
  gr.Markdown("""
339
  **Model Info:**
340
+ - Using Transformers Model
341
+ - Optimized for GPU/CPU
342
+ - Auto device mapping
343
  """)
344
 
345
  response_output = gr.Textbox(
 
419
  ## 🚀 Getting Started
420
 
421
  ### Model:
422
+ **Transformers Model**: Uses HuggingFace transformers library with automatic device mapping
423
 
424
  ### 🔑 API Key Setup:
425
 
 
430
 
431
  ### 📦 Installation Requirements:
432
  ```bash
433
+ pip install gradio transformers langextract pandas requests torch
434
  ```
435
 
436
  ### 🩺 Features:
 
439
  - **Interactive Visualization**: Visual representation of extracted medication entities
440
  - **Quick Questions**: Pre-built common dental questions
441
  - **Customizable Settings**: Adjust response length and creativity
442
+ - **GPU/CPU Support**: Automatic device detection and optimization
443
 
444
  ### ⚠️ Important Disclaimer:
445
  This AI assistant is for educational purposes only. Always consult with a qualified dentist for professional medical advice.
 
451
  <p><strong>⚠️ Disclaimer:</strong> This AI assistant is for educational purposes only.
452
  Always consult with a qualified dentist for professional medical advice.</p>
453
  <p style="text-align: center; margin-top: 1rem;">
454
+ 🦷 Built with Gradio | Gemini | Powered by yasserrmd/DentaInstruct-1.2B
455
  </p>
456
  </div>
457
  """)
 
464
  demo.queue()
465
  demo.launch(
466
  share=False,
467
+ show_error=True
 
468
  )
requirements.txt CHANGED
@@ -1,6 +1,8 @@
1
- gradio==5.39.0
2
- llama-cpp-python==0.3.14
 
3
  langextract==1.0.3
4
  pandas
5
  numpy
6
- requests
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
  langextract==1.0.3
5
  pandas
6
  numpy
7
+ requests
8
+ accelerate