Spaces:
Running
Running
Commit
·
fd19f35
1
Parent(s):
6ab2eb1
deploy login
Browse files- .gitignore +2 -1
- app.py +141 -19
- user_data.json +6 -0
.gitignore
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
.env
|
2 |
-
__pycache__
|
|
|
|
1 |
.env
|
2 |
+
__pycache__
|
3 |
+
flagged
|
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import os
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from google import genai
|
4 |
from google.genai import types
|
@@ -20,7 +22,7 @@ def audio_from_bytes(audio_file_path: str):
|
|
20 |
return f"An error occurred: {e}"
|
21 |
|
22 |
|
23 |
-
def transcribe_and_summarize(audio_file):
|
24 |
"""Processes audio with Gemini API and returns a SOAP summary."""
|
25 |
if audio_file is None:
|
26 |
return "No audio file uploaded."
|
@@ -71,23 +73,143 @@ Identifikasi dan berikan saran dalam bahasa Indonesia tindakan logis selanjutnya
|
|
71 |
|
72 |
# Process the audio
|
73 |
response_text = ""
|
74 |
-
for chunk in client.models.generate_content_stream(
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
):
|
79 |
-
|
80 |
-
|
81 |
-
return response_text
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
demo.launch()
|
|
|
1 |
import os
|
2 |
+
import json
|
3 |
+
import re
|
4 |
import gradio as gr
|
5 |
from google import genai
|
6 |
from google.genai import types
|
|
|
22 |
return f"An error occurred: {e}"
|
23 |
|
24 |
|
25 |
+
def transcribe_and_summarize(audio_file, session):
|
26 |
"""Processes audio with Gemini API and returns a SOAP summary."""
|
27 |
if audio_file is None:
|
28 |
return "No audio file uploaded."
|
|
|
73 |
|
74 |
# Process the audio
|
75 |
response_text = ""
|
76 |
+
# for chunk in client.models.generate_content_stream(
|
77 |
+
# model=model,
|
78 |
+
# contents=contents,
|
79 |
+
# config=generate_content_config,
|
80 |
+
# ):
|
81 |
+
# response_text += chunk.text
|
82 |
+
counter_display, session = click_button(session)
|
83 |
+
return response_text, counter_display, session
|
84 |
+
|
85 |
+
|
86 |
+
DATA_FILE = "user_data.json"
|
87 |
+
|
88 |
+
|
89 |
+
def load_user_data():
|
90 |
+
"""Load user data from JSON file."""
|
91 |
+
if os.path.exists(DATA_FILE):
|
92 |
+
with open(DATA_FILE, "r") as file:
|
93 |
+
return json.load(file)
|
94 |
+
return {} # Return empty dictionary if no data
|
95 |
+
|
96 |
+
|
97 |
+
def save_user_data(username, email):
|
98 |
+
"""Save user data to JSON file with default counter = 10 if new."""
|
99 |
+
data = load_user_data()
|
100 |
+
|
101 |
+
if username not in data: # New user
|
102 |
+
data[username] = {"email": email, "counter": 10} # Set default counter to 10
|
103 |
+
|
104 |
+
with open(DATA_FILE, "w") as file:
|
105 |
+
json.dump(data, file, indent=4)
|
106 |
+
|
107 |
+
return data
|
108 |
+
|
109 |
+
|
110 |
+
def is_valid_wa(username):
|
111 |
+
"""Check if username is all numbers, at least 11 characters, and starts with 08 or 62."""
|
112 |
+
return re.fullmatch(r"^(08|62)\d{9,}$", username) is not None
|
113 |
+
|
114 |
+
|
115 |
+
def is_valid_email(email):
|
116 |
+
"""Check if email format is valid."""
|
117 |
+
return re.fullmatch(r"^[\w\.-]+@[\w\.-]+\.\w+$", email) is not None
|
118 |
+
|
119 |
+
|
120 |
+
def login(username, email, session):
|
121 |
+
"""Handles user login or registration with validation."""
|
122 |
+
|
123 |
+
if not is_valid_wa(username):
|
124 |
+
return (
|
125 |
+
"❌ Invalid WA! Nomor WA minimal 12 digits dimulai '08' atau '62'.",
|
126 |
+
session,
|
127 |
+
gr.update(visible=True),
|
128 |
+
gr.update(visible=False),
|
129 |
+
)
|
130 |
+
|
131 |
+
if not is_valid_email(email):
|
132 |
+
return (
|
133 |
+
"❌ Invalid email format! Please enter a valid email.",
|
134 |
+
session,
|
135 |
+
gr.update(visible=True),
|
136 |
+
gr.update(visible=False),
|
137 |
+
)
|
138 |
+
|
139 |
+
data = save_user_data(username, email) # Save or retrieve user data
|
140 |
+
|
141 |
+
session["username"] = username
|
142 |
+
session["counter"] = data[username]["counter"]
|
143 |
+
|
144 |
+
return "", session, gr.update(visible=False), gr.update(visible=True)
|
145 |
+
|
146 |
+
|
147 |
+
def click_button(session):
|
148 |
+
"""Decrease counter on button click."""
|
149 |
+
if session["counter"] > 0:
|
150 |
+
session["counter"] -= 1
|
151 |
+
|
152 |
+
# Update the user data in JSON file
|
153 |
+
data = load_user_data()
|
154 |
+
data[session["username"]]["counter"] = session["counter"]
|
155 |
+
|
156 |
+
with open(DATA_FILE, "w") as file:
|
157 |
+
json.dump(data, file, indent=4)
|
158 |
+
|
159 |
+
if session["counter"] == 0:
|
160 |
+
return (
|
161 |
+
f"⚠️ Quota habis. Silahkan mengunjungi https://ehealth.co.id atau WA 6285777779926",
|
162 |
+
session,
|
163 |
+
)
|
164 |
+
return f"Counter: {session['counter']}", session
|
165 |
+
else:
|
166 |
+
return (
|
167 |
+
f"⚠️ Quota habis. Silahkan mengunjungi https://ehealth.co.id atau WA 6285777779926",
|
168 |
+
session,
|
169 |
+
)
|
170 |
+
|
171 |
+
|
172 |
+
# Gradio Interface
|
173 |
+
with gr.Blocks() as demo:
|
174 |
+
session = gr.State({"username": None, "counter": 0}) # Manage session state
|
175 |
+
|
176 |
+
gr.Markdown("# 🛡️ eHealth.co.id Scribe")
|
177 |
+
|
178 |
+
# Login Section
|
179 |
+
login_block = gr.Column(visible=True)
|
180 |
+
with login_block:
|
181 |
+
email_input = gr.Textbox(label="Email")
|
182 |
+
username_input = gr.Textbox(label="WA", type="password") # Hide input
|
183 |
+
login_button = gr.Button("🔑 Login / Register")
|
184 |
+
gr.Markdown(
|
185 |
+
"""### dengan login saya menyetujui ketentuan penggunaan data eHealth.co.id dan tidak akan menuntut perusahaan eHealth.co.id untuk uji coba gratis AI Transcribe ini
|
186 |
+
|
187 |
+
saya memahami bahwa tidak ada data suara maupun tulisan medis yang akan disimpan oleh perusahaan
|
188 |
+
|
189 |
+
seluruh data yang saya sediakan adalah data yang benar dan tidak bermaksud melanggar hukum
|
190 |
+
|
191 |
+
## Setelah quota habis, Silahkan mengunjungi https://ehealth.co.id atau WA 6285777779926"""
|
192 |
+
)
|
193 |
+
output_text = gr.Textbox(label="Status", interactive=False)
|
194 |
+
|
195 |
+
# Main User Interface (After Login)
|
196 |
+
user_block = gr.Column(visible=False)
|
197 |
+
with user_block:
|
198 |
+
counter_display = gr.Textbox(label="Status Message", interactive=False)
|
199 |
+
gr.Interface(
|
200 |
+
fn=transcribe_and_summarize,
|
201 |
+
inputs=[gr.Audio(type="filepath", sources="microphone"), session],
|
202 |
+
outputs=["text", counter_display, session],
|
203 |
+
title="eH Transcription",
|
204 |
+
description="pastikan HP/Laptop memiliki microphone untuk merekam percakapan dokter-pasien menjadi rekam medis SOAP",
|
205 |
+
allow_flagging="never",
|
206 |
+
)
|
207 |
+
|
208 |
+
# Login button action
|
209 |
+
login_button.click(
|
210 |
+
login,
|
211 |
+
[username_input, email_input, session],
|
212 |
+
[output_text, session, login_block, user_block],
|
213 |
+
)
|
214 |
|
215 |
demo.launch()
|
user_data.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"6285777779926": {
|
3 |
+
"email": "[email protected]",
|
4 |
+
"counter": 10
|
5 |
+
}
|
6 |
+
}
|