File size: 11,517 Bytes
32519eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
"""
Synthex Medical Text Generator - MVP Streamlit App
Deploy this on Hugging Face Spaces for free hosting
"""

import streamlit as st
import json
import time
from datetime import datetime
import pandas as pd
import os
import sys
import logging

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Add src directory to Python path
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))

# Import the medical generator
from src.generation.medical_generator import MedicalTextGenerator, DEFAULT_GEMINI_API_KEY

# Page config
st.set_page_config(
    page_title="Synthex Medical Text Generator",
    page_icon="πŸ₯",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS
st.markdown("""
<style>
/* Main container styling */
.main {
    padding: 2rem;
    background-color: #f8f9fa;
}

/* Header styling */
.main-header {
    font-size: 2.5rem;
    font-weight: bold;
    color: #1f77b4;
    text-align: center;
    margin-bottom: 1rem;
    padding: 1rem;
    background: linear-gradient(135deg, #1f77b4 0%, #2c9cdb 100%);
    color: white;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.sub-header {
    font-size: 1.2rem;
    color: #666;
    text-align: center;
    margin-bottom: 2rem;
    padding: 0.5rem;
}

/* Card styling */
.record-container {
    background-color: white;
    padding: 1.5rem;
    border-radius: 10px;
    border-left: 4px solid #1f77b4;
    margin: 1rem 0;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    transition: transform 0.2s;
}

.record-container:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* Stats container styling */
.stats-container {
    background-color: white;
    padding: 1.5rem;
    border-radius: 10px;
    margin: 1rem 0;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* Button styling */
.stButton>button {
    width: 100%;
    border-radius: 5px;
    height: 3em;
    font-weight: bold;
    transition: all 0.3s;
}

.stButton>button:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* Metric styling */
.stMetric {
    background-color: #f8f9fa;
    padding: 1rem;
    border-radius: 5px;
    text-align: center;
}

/* Sidebar styling */
.sidebar .sidebar-content {
    background-color: #f8f9fa;
}

/* Progress bar styling */
.stProgress > div > div {
    background-color: #1f77b4;
}

/* Success message styling */
.stSuccess {
    padding: 1rem;
    border-radius: 5px;
    background-color: #d4edda;
    color: #155724;
    margin: 1rem 0;
}

/* Error message styling */
.stError {
    padding: 1rem;
    border-radius: 5px;
    background-color: #f8d7da;
    color: #721c24;
    margin: 1rem 0;
}

/* Expander styling */
.streamlit-expanderHeader {
    font-size: 1.1rem;
    font-weight: bold;
    color: #1f77b4;
}

/* Text area styling */
.stTextArea textarea {
    font-family: monospace;
    font-size: 0.9rem;
    line-height: 1.5;
}
</style>
""", unsafe_allow_html=True)

# Initialize session state
if 'generated_records' not in st.session_state:
    st.session_state.generated_records = []
if 'total_generated' not in st.session_state:
    st.session_state.total_generated = 0
if 'generator' not in st.session_state:
    st.session_state.generator = None

# Header
st.markdown('<div class="main-header">πŸ₯ Synthex Medical Text Generator</div>', unsafe_allow_html=True)
st.markdown('<div class="sub-header">Generate synthetic medical records for AI training and testing</div>', unsafe_allow_html=True)

# Add a status message area
status_area = st.empty()

# Sidebar
with st.sidebar:
    st.markdown("### βš™οΈ Configuration")
    
    # API Key section
    with st.expander("πŸ”‘ API Settings", expanded=False):
        gemini_api_key = st.text_input(
            "Gemini API Key", 
            value=os.getenv('GEMINI_API_KEY', ''),
            type="password",
            help="Enter your Google Gemini API key for better generation quality"
        )
    
    # Record settings
    st.markdown("### πŸ“ Record Settings")
    record_type = st.selectbox(
        "Select Record Type",
        ["clinical_note", "discharge_summary", "lab_report", "prescription", "patient_intake"],
        format_func=lambda x: x.replace("_", " ").title()
    )
    
    quantity = st.slider("Number of Records", 1, 20, 5)
    
    # Generation settings
    st.markdown("### πŸ€– Generation Settings")
    use_gemini = st.checkbox(
        "Use Gemini API", 
        value=False,
        help="Uses Google Gemini API for better quality generation"
    )
    
    # Advanced options
    with st.expander("⚑ Advanced Options"):
        include_metadata = st.checkbox("Include Metadata", value=True)
        export_format = st.selectbox("Export Format", ["JSON", "CSV", "TXT"])

# Main content with better organization
col1, col2 = st.columns([2, 1])

with col1:
    st.markdown("### πŸ“ Generate Records")
    
    # Generation button with better styling
    if st.button("πŸš€ Generate Records", type="primary", use_container_width=True):
        status_area.info("Initializing generator...")
        
        # Initialize generator if not already done
        if st.session_state.generator is None:
            try:
                with st.spinner("Initializing medical text generator..."):
                    st.session_state.generator = MedicalTextGenerator(gemini_api_key=gemini_api_key)
                status_area.success("Generator initialized successfully!")
            except Exception as e:
                status_area.error(f"Error initializing generator: {str(e)}")
                st.stop()
        
        # Generate records with progress
        progress_bar = st.progress(0)
        status_text = st.empty()
        
        generated_records = []
        
        for i in range(quantity):
            status_text.text(f"Generating record {i+1} of {quantity}...")
            progress_bar.progress((i + 1) / quantity)
            
            try:
                record = st.session_state.generator.generate_record(record_type, use_gemini=use_gemini)
                generated_records.append(record)
                
                # Rate limiting
                if use_gemini:
                    time.sleep(1)
                    
            except Exception as e:
                logger.error(f"Failed to generate record {i+1}: {str(e)}")
                status_area.error(f"Failed to generate record {i+1}: {str(e)}")
                continue
        
        # Update session state
        if generated_records:
            st.session_state.generated_records.extend(generated_records)
            st.session_state.total_generated += len(generated_records)
            
            status_text.text("βœ… Generation complete!")
            progress_bar.progress(1.0)
            
            status_area.success(f"Successfully generated {len(generated_records)} medical records!")
    
    # Display generated records with better organization
    if st.session_state.generated_records:
        st.markdown("### πŸ“‹ Generated Records")
        
        # Filters with better layout
        col_filter1, col_filter2 = st.columns(2)
        with col_filter1:
            filter_type = st.selectbox(
                "Filter by Type", 
                ["All"] + list(set([r.get('type', 'Unknown') for r in st.session_state.generated_records]))
            )
        with col_filter2:
            records_per_page = st.selectbox("Records per page", [5, 10, 20, 50])
        
        # Filter records
        filtered_records = st.session_state.generated_records
        if filter_type != "All":
            filtered_records = [r for r in filtered_records if r.get('type', 'Unknown') == filter_type]
        
        # Pagination
        total_records = len(filtered_records)
        total_pages = (total_records - 1) // records_per_page + 1
        
        if total_pages > 1:
            page = st.selectbox("Page", range(1, total_pages + 1))
            start_idx = (page - 1) * records_per_page
            end_idx = start_idx + records_per_page
            page_records = filtered_records[start_idx:end_idx]
        else:
            page_records = filtered_records
        
        # Display records with better styling
        for i, record in enumerate(page_records):
            with st.expander(f"Record {record.get('id', 'Unknown')} - {record.get('type', 'Unknown').replace('_', ' ').title()}"):
                if include_metadata:
                    col_meta1, col_meta2, col_meta3 = st.columns(3)
                    with col_meta1:
                        st.metric("Type", record.get('type', 'Unknown').replace('_', ' ').title())
                    with col_meta2:
                        st.metric("Generated", record.get('timestamp', 'N/A'))
                    with col_meta3:
                        st.metric("Source", record.get('source', 'Hugging Face'))
                
                st.markdown('<div class="record-container">', unsafe_allow_html=True)
                st.text_area("Content", record.get('text', 'No content available'), height=200, key=f"record_{i}")
                st.markdown('</div>', unsafe_allow_html=True)

with col2:
    st.markdown("### πŸ“Š Statistics")
    
    # Stats container with better styling
    st.markdown('<div class="stats-container">', unsafe_allow_html=True)
    
    # Total records
    st.metric("Total Records Generated", st.session_state.total_generated)
    
    # Record type distribution with better visualization
    if st.session_state.generated_records:
        type_counts = pd.Series([r.get('type', 'Unknown') for r in st.session_state.generated_records]).value_counts()
        st.markdown("#### Record Type Distribution")
        st.bar_chart(type_counts)
    
    # Export options with better organization
    st.markdown("#### πŸ’Ύ Export Data")
    if st.session_state.generated_records:
        if export_format == "JSON":
            json_str = json.dumps(st.session_state.generated_records, indent=2)
            st.download_button(
                "πŸ“₯ Download JSON",
                json_str,
                file_name=f"medical_records_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
                mime="application/json",
                use_container_width=True
            )
        elif export_format == "CSV":
            df = pd.DataFrame(st.session_state.generated_records)
            csv = df.to_csv(index=False)
            st.download_button(
                "πŸ“₯ Download CSV",
                csv,
                file_name=f"medical_records_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
                mime="text/csv",
                use_container_width=True
            )
        elif export_format == "TXT":
            txt = "\n\n".join([f"Record {r.get('id', 'Unknown')} ({r.get('type', 'Unknown')}):\n{r.get('text', 'No content available')}" for r in st.session_state.generated_records])
            st.download_button(
                "πŸ“₯ Download TXT",
                txt,
                file_name=f"medical_records_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt",
                mime="text/plain",
                use_container_width=True
            )
    
    st.markdown('</div>', unsafe_allow_html=True)

# Add a footer
st.markdown("---")
st.markdown("""
<div style='text-align: center; color: #666;'>
    <p>Built with ❀️ using Streamlit | Synthex Medical Text Generator</p>
</div>
""", unsafe_allow_html=True)