Spaces:
Sleeping
Sleeping
File size: 10,409 Bytes
59c7f4b |
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 |
"""
Context Cruncher - Gradio Application
Extract structured context data from voice recordings using Gemini AI.
"""
import gradio as gr
import os
from pathlib import Path
import tempfile
from dotenv import load_dotenv
from gemini_processor import (
process_audio_with_gemini,
create_markdown_file,
create_json_file
)
# Load environment variables
load_dotenv()
def process_audio(
audio_input,
uploaded_file,
api_key: str,
user_identification: str,
user_name: str = ""
) -> tuple:
"""
Process audio from either recording or upload.
Args:
audio_input: Audio from microphone recording
uploaded_file: Uploaded audio file
api_key: Gemini API key
user_identification: "name" or "user"
user_name: User's name if using name identification
Returns:
Tuple of (markdown_content, markdown_file, json_file, status_message)
"""
try:
# Validate API key
if not api_key or api_key.strip() == "":
return (
"",
None,
None,
"Error: Please provide a Gemini API key"
)
# Determine which audio source to use
audio_path = None
if audio_input is not None:
audio_path = audio_input
elif uploaded_file is not None:
audio_path = uploaded_file.name
if audio_path is None:
return (
"",
None,
None,
"Error: Please record audio or upload an audio file"
)
# Determine user reference
user_ref = None
if user_identification == "name":
if not user_name or user_name.strip() == "":
return (
"",
None,
None,
"Error: Please provide your name when using name identification"
)
user_ref = user_name.strip()
# Process with Gemini
status_msg = "Processing audio with Gemini API..."
context_markdown, human_readable_name, snake_case_filename = process_audio_with_gemini(
audio_path,
api_key,
user_ref
)
# Create output files
md_filename, md_content = create_markdown_file(
context_markdown,
human_readable_name,
snake_case_filename
)
json_filename, json_content = create_json_file(
context_markdown,
human_readable_name,
snake_case_filename
)
# Write files to temp directory for download
temp_dir = tempfile.mkdtemp()
md_path = Path(temp_dir) / md_filename
json_path = Path(temp_dir) / json_filename
with open(md_path, 'w') as f:
f.write(md_content)
with open(json_path, 'w') as f:
f.write(json_content)
return (
md_content,
str(md_path),
str(json_path),
f"Success! Context extracted: {human_readable_name}"
)
except Exception as e:
return (
"",
None,
None,
f"Error: {str(e)}"
)
# Custom CSS for better styling
custom_css = """
.gradio-container {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}
.main-header {
text-align: center;
margin-bottom: 1.5rem;
padding-bottom: 1rem;
border-bottom: 2px solid #e5e7eb;
}
.main-header h1 {
font-size: 2rem;
font-weight: 600;
color: #1f2937;
margin-bottom: 0.5rem;
}
.main-header p {
color: #6b7280;
font-size: 1rem;
}
.section-header {
font-weight: 600;
color: #374151;
margin-bottom: 1rem;
}
"""
# Create Gradio interface
with gr.Blocks(css=custom_css, title="Context Cruncher") as demo:
gr.Markdown(
"""
# Context Cruncher
Extract structured context data from voice recordings using AI
""",
elem_classes="main-header"
)
with gr.Tabs():
with gr.Tab("Extract"):
with gr.Row():
with gr.Column(scale=1):
with gr.Accordion("Configuration", open=True):
api_key_input = gr.Textbox(
label="Gemini API Key",
placeholder="Enter your Gemini API key",
type="password",
value=os.getenv("GEMINI_API", ""),
info="Get your API key from https://ai.google.dev/"
)
user_identification = gr.Radio(
choices=["user", "name"],
value="user",
label="User Identification",
info="How should you be referred to in the context data?"
)
user_name_input = gr.Textbox(
label="Your Name",
placeholder="Enter your name",
visible=False,
info="Used when 'name' is selected above"
)
gr.Markdown("### Audio Input", elem_classes="section-header")
audio_recording = gr.Audio(
sources=["microphone"],
type="filepath",
label="Record Audio"
)
gr.Markdown("**OR**")
audio_upload = gr.File(
label="Upload Audio File",
file_types=["audio"],
type="filepath"
)
process_btn = gr.Button("Extract Context", variant="primary", size="lg")
with gr.Column(scale=1):
gr.Markdown("### Results", elem_classes="section-header")
status_output = gr.Textbox(
label="Status",
interactive=False,
show_label=True
)
context_display = gr.Textbox(
label="Context Data (Markdown)",
lines=18,
interactive=False,
show_copy_button=True
)
with gr.Row():
markdown_download = gr.File(label="Download Markdown")
json_download = gr.File(label="Download JSON")
with gr.Tab("About"):
gr.Markdown(
"""
## What is Context Cruncher?
Context Cruncher transforms casual voice recordings into clean, structured context data
that AI systems can use for personalization.
**Context data** refers to specific information about users that grounds AI inference
for more personalized results.
## How It Works
1. **Configure** - Enter your Gemini API key and choose how you want to be identified
2. **Input Audio** - Either record directly in your browser or upload an audio file (MP3, WAV, OPUS)
3. **Extract** - Click the button and let AI clean up your recording into structured context data
4. **Download** - Get your context data as Markdown or JSON, or copy directly from the text area
## What Gets Extracted
This tool processes your audio by:
- Removing irrelevant information and tangents
- Eliminating duplicates and redundancy
- Reformatting from first person to third person
- Organizing information hierarchically
- Outputting both Markdown and JSON formats
## Example Transformation
**Raw Audio:**
> "Okay so... let's document my health problems... I've had asthma since I was a kid.
> I take a daily inhaler called Relvar for that. Oh hey Jay! What's up!
> Okay, where was I... I also take Vyvanse for ADHD."
**Structured Output:**
```markdown
## Medical Conditions
- the user has had asthma since childhood
- the user has adult ADHD
## Medication List
- the user takes Relvar, daily, for asthma
- the user takes Vyvanse for ADHD
```
## Privacy Notice
Your audio is processed using the Gemini API. Review Google's privacy policies
before using this tool with sensitive information.
## Technical Details
- **AI Model**: Gemini 2.0 Flash (multimodal audio understanding)
- **Processing**: Direct audio file upload to Gemini API
- **Output Formats**: Markdown and JSON
## Use Cases
- AI assistant personalization
- Knowledge management
- Preference mapping
- Medical history documentation (note privacy considerations)
- Project context capture
"""
)
# Show/hide name input based on identification method
def toggle_name_input(identification_choice):
return gr.update(visible=identification_choice == "name")
user_identification.change(
fn=toggle_name_input,
inputs=[user_identification],
outputs=[user_name_input]
)
# Process button click
process_btn.click(
fn=process_audio,
inputs=[
audio_recording,
audio_upload,
api_key_input,
user_identification,
user_name_input
],
outputs=[
context_display,
markdown_download,
json_download,
status_output
]
)
if __name__ == "__main__":
# For Hugging Face Spaces, share should be False
# Set server_name to 0.0.0.0 for Spaces compatibility
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)
|