Update app.py
Browse files
app.py
CHANGED
|
@@ -7,34 +7,31 @@ import spaces
|
|
| 7 |
warnings.filterwarnings("ignore")
|
| 8 |
|
| 9 |
|
| 10 |
-
print("Loading Whisper (transcription)...")
|
| 11 |
-
whisper_model = whisper.load_model("tiny")
|
| 12 |
-
|
| 13 |
-
model_name = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 14 |
-
print(f"Loading {model_name} (reflection)...")
|
| 15 |
-
generator = pipeline(
|
| 16 |
-
"text-generation",
|
| 17 |
-
model=model_name,
|
| 18 |
-
device_map="auto",
|
| 19 |
-
dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
print("Models ready.")
|
| 23 |
|
| 24 |
@spaces.GPU
|
| 25 |
def transcribe(audio_path: str) -> str:
|
| 26 |
-
"""Transcribe audio file to text using Whisper."""
|
| 27 |
if audio_path is None:
|
| 28 |
return ""
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return result["text"].strip()
|
| 31 |
|
|
|
|
| 32 |
@spaces.GPU
|
| 33 |
def reflect(transcript: str) -> str:
|
| 34 |
-
"""Generate a journaling reflection using Qwen."""
|
| 35 |
if not transcript:
|
| 36 |
-
return "No transcript
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
prompt = f"""You are a warm, thoughtful journaling companion. Your tone is human and gentle, never clinical or robotic.
|
| 39 |
|
| 40 |
The user just shared this voice journal entry:
|
|
@@ -72,7 +69,8 @@ Keep the full response under 160 words. Be kind, specific, and real."""
|
|
| 72 |
else:
|
| 73 |
# Fallback: strip the prompt
|
| 74 |
reply = generated[len(prompt):]
|
| 75 |
-
|
|
|
|
| 76 |
return reply.strip()
|
| 77 |
|
| 78 |
|
|
|
|
| 7 |
warnings.filterwarnings("ignore")
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@spaces.GPU
|
| 12 |
def transcribe(audio_path: str) -> str:
|
|
|
|
| 13 |
if audio_path is None:
|
| 14 |
return ""
|
| 15 |
+
print("Loading Whisper inside GPU...")
|
| 16 |
+
model = whisper.load_model("tiny")
|
| 17 |
+
result = model.transcribe(audio_path)
|
| 18 |
+
del model
|
| 19 |
+
torch.cuda.empty_cache() if torch.cuda.is_available() else None
|
| 20 |
return result["text"].strip()
|
| 21 |
|
| 22 |
+
|
| 23 |
@spaces.GPU
|
| 24 |
def reflect(transcript: str) -> str:
|
|
|
|
| 25 |
if not transcript:
|
| 26 |
+
return "No transcript..."
|
| 27 |
+
|
| 28 |
+
print("Loading Qwen inside GPU...")
|
| 29 |
+
generator = pipeline(
|
| 30 |
+
"text-generation",
|
| 31 |
+
model="Qwen/Qwen2.5-0.5B-Instruct",
|
| 32 |
+
device_map="auto",
|
| 33 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 34 |
+
)
|
| 35 |
prompt = f"""You are a warm, thoughtful journaling companion. Your tone is human and gentle, never clinical or robotic.
|
| 36 |
|
| 37 |
The user just shared this voice journal entry:
|
|
|
|
| 69 |
else:
|
| 70 |
# Fallback: strip the prompt
|
| 71 |
reply = generated[len(prompt):]
|
| 72 |
+
del generator
|
| 73 |
+
torch.cuda.empty_cache() if torch.cuda.is_available() else None
|
| 74 |
return reply.strip()
|
| 75 |
|
| 76 |
|