Spaces:
Running
Running
zach
commited on
Commit
·
5a007ca
1
Parent(s):
b2c4d0e
Clean up docstrings across files
Browse files- src/app.py +4 -2
- src/config.py +1 -0
- src/constants.py +1 -3
- src/integrations/anthropic_api.py +4 -3
- src/integrations/elevenlabs_api.py +3 -1
- src/integrations/hume_api.py +3 -1
- src/theme.py +3 -17
- src/utils.py +3 -10
src/app.py
CHANGED
|
@@ -4,16 +4,18 @@ app.py
|
|
| 4 |
Gradio UI for interacting with the Anthropic API, Hume TTS API, and ElevenLabs TTS API.
|
| 5 |
|
| 6 |
Users enter a prompt, which is processed using Claude by Anthropic to generate text.
|
| 7 |
-
The text is then synthesized into speech using both Hume and ElevenLabs TTS APIs.
|
| 8 |
-
Users can compare the outputs in an interactive UI.
|
| 9 |
"""
|
| 10 |
|
| 11 |
# Standard Library Imports
|
| 12 |
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
import random
|
| 14 |
from typing import Union, Tuple
|
|
|
|
| 15 |
# Third-Party Library Imports
|
| 16 |
import gradio as gr
|
|
|
|
| 17 |
# Local Application Imports
|
| 18 |
from src.config import logger
|
| 19 |
from src.constants import (
|
|
|
|
| 4 |
Gradio UI for interacting with the Anthropic API, Hume TTS API, and ElevenLabs TTS API.
|
| 5 |
|
| 6 |
Users enter a prompt, which is processed using Claude by Anthropic to generate text.
|
| 7 |
+
The text is then synthesized into speech using both Hume and ElevenLabs text-to-speech (TTS) APIs.
|
| 8 |
+
Users can compare the outputs and vote for their favorite in an interactive UI.
|
| 9 |
"""
|
| 10 |
|
| 11 |
# Standard Library Imports
|
| 12 |
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
import random
|
| 14 |
from typing import Union, Tuple
|
| 15 |
+
|
| 16 |
# Third-Party Library Imports
|
| 17 |
import gradio as gr
|
| 18 |
+
|
| 19 |
# Local Application Imports
|
| 20 |
from src.config import logger
|
| 21 |
from src.constants import (
|
src/config.py
CHANGED
|
@@ -12,6 +12,7 @@ Key Features:
|
|
| 12 |
# Standard Library Imports
|
| 13 |
import logging
|
| 14 |
import os
|
|
|
|
| 15 |
# Third-Party Library Imports
|
| 16 |
from dotenv import load_dotenv
|
| 17 |
|
|
|
|
| 12 |
# Standard Library Imports
|
| 13 |
import logging
|
| 14 |
import os
|
| 15 |
+
|
| 16 |
# Third-Party Library Imports
|
| 17 |
from dotenv import load_dotenv
|
| 18 |
|
src/constants.py
CHANGED
|
@@ -4,11 +4,9 @@ constants.py
|
|
| 4 |
This module defines global constants used throughout the project.
|
| 5 |
"""
|
| 6 |
|
| 7 |
-
#
|
| 8 |
PROMPT_MIN_LENGTH: int = 10
|
| 9 |
PROMPT_MAX_LENGTH: int = 300
|
| 10 |
-
|
| 11 |
-
# Vote button constants
|
| 12 |
OPTION_ONE: str = "Option 1"
|
| 13 |
OPTION_TWO: str = "Option 2"
|
| 14 |
TROPHY_EMOJI = "🏆"
|
|
|
|
| 4 |
This module defines global constants used throughout the project.
|
| 5 |
"""
|
| 6 |
|
| 7 |
+
# UI constants
|
| 8 |
PROMPT_MIN_LENGTH: int = 10
|
| 9 |
PROMPT_MAX_LENGTH: int = 300
|
|
|
|
|
|
|
| 10 |
OPTION_ONE: str = "Option 1"
|
| 11 |
OPTION_TWO: str = "Option 2"
|
| 12 |
TROPHY_EMOJI = "🏆"
|
src/integrations/anthropic_api.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
"""
|
| 2 |
anthropic_api.py
|
| 3 |
|
| 4 |
-
This file defines the interaction with the Anthropic API, focusing on generating text
|
| 5 |
-
|
| 6 |
-
and processing API responses.
|
| 7 |
|
| 8 |
Key Features:
|
| 9 |
- Encapsulates all logic related to the Anthropic API.
|
|
@@ -23,10 +22,12 @@ Functions:
|
|
| 23 |
from dataclasses import dataclass
|
| 24 |
import logging
|
| 25 |
from typing import List, Optional, Union
|
|
|
|
| 26 |
# Third-Party Library Imports
|
| 27 |
from anthropic import Anthropic
|
| 28 |
from anthropic.types import Message, ModelParam, TextBlock
|
| 29 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
|
|
|
| 30 |
# Local Application Imports
|
| 31 |
from src.config import logger
|
| 32 |
from src.utils import truncate_text, validate_env_var
|
|
|
|
| 1 |
"""
|
| 2 |
anthropic_api.py
|
| 3 |
|
| 4 |
+
This file defines the interaction with the Anthropic API, focusing on generating text using the Claude model.
|
| 5 |
+
It includes functionality for input validation, API request handling, and processing API responses.
|
|
|
|
| 6 |
|
| 7 |
Key Features:
|
| 8 |
- Encapsulates all logic related to the Anthropic API.
|
|
|
|
| 22 |
from dataclasses import dataclass
|
| 23 |
import logging
|
| 24 |
from typing import List, Optional, Union
|
| 25 |
+
|
| 26 |
# Third-Party Library Imports
|
| 27 |
from anthropic import Anthropic
|
| 28 |
from anthropic.types import Message, ModelParam, TextBlock
|
| 29 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
| 30 |
+
|
| 31 |
# Local Application Imports
|
| 32 |
from src.config import logger
|
| 33 |
from src.utils import truncate_text, validate_env_var
|
src/integrations/elevenlabs_api.py
CHANGED
|
@@ -12,7 +12,7 @@ Key Features:
|
|
| 12 |
|
| 13 |
Classes:
|
| 14 |
- ElevenLabsConfig: Immutable configuration for interacting with the TTS API.
|
| 15 |
-
- ElevenLabsError: Custom exception for
|
| 16 |
|
| 17 |
Functions:
|
| 18 |
- text_to_speech_with_elevenlabs: Converts text to speech using the ElevenLabs TTS API.
|
|
@@ -23,9 +23,11 @@ from dataclasses import dataclass
|
|
| 23 |
import logging
|
| 24 |
import random
|
| 25 |
from typing import Optional
|
|
|
|
| 26 |
# Third-Party Library Imports
|
| 27 |
from elevenlabs import ElevenLabs
|
| 28 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
|
|
|
| 29 |
# Local Application Imports
|
| 30 |
from src.config import logger
|
| 31 |
from src.utils import validate_env_var, truncate_text
|
|
|
|
| 12 |
|
| 13 |
Classes:
|
| 14 |
- ElevenLabsConfig: Immutable configuration for interacting with the TTS API.
|
| 15 |
+
- ElevenLabsError: Custom exception for ElevenLabs API-related errors.
|
| 16 |
|
| 17 |
Functions:
|
| 18 |
- text_to_speech_with_elevenlabs: Converts text to speech using the ElevenLabs TTS API.
|
|
|
|
| 23 |
import logging
|
| 24 |
import random
|
| 25 |
from typing import Optional
|
| 26 |
+
|
| 27 |
# Third-Party Library Imports
|
| 28 |
from elevenlabs import ElevenLabs
|
| 29 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
| 30 |
+
|
| 31 |
# Local Application Imports
|
| 32 |
from src.config import logger
|
| 33 |
from src.utils import validate_env_var, truncate_text
|
src/integrations/hume_api.py
CHANGED
|
@@ -12,7 +12,7 @@ Key Features:
|
|
| 12 |
|
| 13 |
Classes:
|
| 14 |
- HumeConfig: Immutable configuration for interacting with the TTS API.
|
| 15 |
-
- HumeError: Custom exception for
|
| 16 |
|
| 17 |
Functions:
|
| 18 |
- text_to_speech_with_hume: Converts text to speech using the Hume TTS API with input validation and retry logic.
|
|
@@ -23,9 +23,11 @@ from dataclasses import dataclass
|
|
| 23 |
import logging
|
| 24 |
import random
|
| 25 |
from typing import List, Optional
|
|
|
|
| 26 |
# Third-Party Library Imports
|
| 27 |
import requests
|
| 28 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
|
|
|
| 29 |
# Local Application Imports
|
| 30 |
from src.config import logger
|
| 31 |
from src.utils import validate_env_var, truncate_text
|
|
|
|
| 12 |
|
| 13 |
Classes:
|
| 14 |
- HumeConfig: Immutable configuration for interacting with the TTS API.
|
| 15 |
+
- HumeError: Custom exception for Hume API-related errors.
|
| 16 |
|
| 17 |
Functions:
|
| 18 |
- text_to_speech_with_hume: Converts text to speech using the Hume TTS API with input validation and retry logic.
|
|
|
|
| 23 |
import logging
|
| 24 |
import random
|
| 25 |
from typing import List, Optional
|
| 26 |
+
|
| 27 |
# Third-Party Library Imports
|
| 28 |
import requests
|
| 29 |
from tenacity import retry, stop_after_attempt, wait_fixed, before_log, after_log
|
| 30 |
+
|
| 31 |
# Local Application Imports
|
| 32 |
from src.config import logger
|
| 33 |
from src.utils import validate_env_var, truncate_text
|
src/theme.py
CHANGED
|
@@ -2,21 +2,14 @@
|
|
| 2 |
theme.py
|
| 3 |
|
| 4 |
Defines a custom Gradio theme.
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
and overrides default styling variables. It uses CSS variables for consistency
|
| 8 |
-
with the application's styling.
|
| 9 |
-
|
| 10 |
-
Key Features:
|
| 11 |
-
- Defines a color palette using CSS variables.
|
| 12 |
-
- Customizes styling for Gradio components (buttons, inputs, etc.).
|
| 13 |
-
- Adjusts shadows, borders, and gradients.
|
| 14 |
-
- Supports light and dark modes.
|
| 15 |
"""
|
| 16 |
|
| 17 |
# Standard Library Imports
|
| 18 |
from __future__ import annotations
|
| 19 |
from collections.abc import Iterable
|
|
|
|
| 20 |
# Third-Party Library Imports
|
| 21 |
from gradio.themes.base import Base
|
| 22 |
from gradio.themes.utils import colors, fonts, sizes
|
|
@@ -64,16 +57,13 @@ class CustomTheme(Base):
|
|
| 64 |
error_border_color_dark='#EF4444',
|
| 65 |
error_icon_color='#B91C1C',
|
| 66 |
error_icon_color_dark='#EF4444',
|
| 67 |
-
|
| 68 |
# Shadows
|
| 69 |
input_shadow_focus='0 0 0 *shadow_spread #7C3AED80, *shadow_inset',
|
| 70 |
input_shadow_focus_dark='0 0 0 *shadow_spread #40404080, *shadow_inset',
|
| 71 |
-
|
| 72 |
# Button borders
|
| 73 |
button_border_width='0px',
|
| 74 |
input_border_width='1px',
|
| 75 |
input_background_fill='#F9FAFB',
|
| 76 |
-
|
| 77 |
# Gradients
|
| 78 |
stat_background_fill='linear-gradient(to right, #7C3AED, #D8B4FE)',
|
| 79 |
stat_background_fill_dark='linear-gradient(to right, #7C3AED, #5B21B6)',
|
|
@@ -81,7 +71,6 @@ class CustomTheme(Base):
|
|
| 81 |
checkbox_label_background_fill_dark='#1F2937',
|
| 82 |
checkbox_label_background_fill_hover='#E5E7EB',
|
| 83 |
checkbox_label_background_fill_hover_dark='#374151',
|
| 84 |
-
|
| 85 |
# Primary Button
|
| 86 |
button_primary_background_fill='#111111',
|
| 87 |
button_primary_background_fill_dark='#171717',
|
|
@@ -89,7 +78,6 @@ class CustomTheme(Base):
|
|
| 89 |
button_primary_background_fill_hover_dark='#3F3F3F',
|
| 90 |
button_primary_text_color='#FFFFFF',
|
| 91 |
button_primary_text_color_dark='#FFFFFF',
|
| 92 |
-
|
| 93 |
# Secondary Button
|
| 94 |
button_secondary_background_fill='#E5E7EB',
|
| 95 |
button_secondary_background_fill_dark='#4B5563',
|
|
@@ -97,7 +85,6 @@ class CustomTheme(Base):
|
|
| 97 |
button_secondary_background_fill_hover_dark='#374151',
|
| 98 |
button_secondary_text_color='#111827',
|
| 99 |
button_secondary_text_color_dark='#FFFFFF',
|
| 100 |
-
|
| 101 |
# Cancel Button
|
| 102 |
button_cancel_background_fill='#EF4444',
|
| 103 |
button_cancel_background_fill_dark='#B91C1C',
|
|
@@ -107,7 +94,6 @@ class CustomTheme(Base):
|
|
| 107 |
button_cancel_text_color_dark='#FFFFFF',
|
| 108 |
button_cancel_text_color_hover='#FFFFFF',
|
| 109 |
button_cancel_text_color_hover_dark='#FFFFFF',
|
| 110 |
-
|
| 111 |
# Other
|
| 112 |
border_color_accent_subdued='#A78BFA',
|
| 113 |
)
|
|
|
|
| 2 |
theme.py
|
| 3 |
|
| 4 |
Defines a custom Gradio theme.
|
| 5 |
+
- For more information on Gradio themes see: https://www.gradio.app/docs/gradio/themes
|
| 6 |
+
- For manual styling with css, see /src/assets/styles.css
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
# Standard Library Imports
|
| 10 |
from __future__ import annotations
|
| 11 |
from collections.abc import Iterable
|
| 12 |
+
|
| 13 |
# Third-Party Library Imports
|
| 14 |
from gradio.themes.base import Base
|
| 15 |
from gradio.themes.utils import colors, fonts, sizes
|
|
|
|
| 57 |
error_border_color_dark='#EF4444',
|
| 58 |
error_icon_color='#B91C1C',
|
| 59 |
error_icon_color_dark='#EF4444',
|
|
|
|
| 60 |
# Shadows
|
| 61 |
input_shadow_focus='0 0 0 *shadow_spread #7C3AED80, *shadow_inset',
|
| 62 |
input_shadow_focus_dark='0 0 0 *shadow_spread #40404080, *shadow_inset',
|
|
|
|
| 63 |
# Button borders
|
| 64 |
button_border_width='0px',
|
| 65 |
input_border_width='1px',
|
| 66 |
input_background_fill='#F9FAFB',
|
|
|
|
| 67 |
# Gradients
|
| 68 |
stat_background_fill='linear-gradient(to right, #7C3AED, #D8B4FE)',
|
| 69 |
stat_background_fill_dark='linear-gradient(to right, #7C3AED, #5B21B6)',
|
|
|
|
| 71 |
checkbox_label_background_fill_dark='#1F2937',
|
| 72 |
checkbox_label_background_fill_hover='#E5E7EB',
|
| 73 |
checkbox_label_background_fill_hover_dark='#374151',
|
|
|
|
| 74 |
# Primary Button
|
| 75 |
button_primary_background_fill='#111111',
|
| 76 |
button_primary_background_fill_dark='#171717',
|
|
|
|
| 78 |
button_primary_background_fill_hover_dark='#3F3F3F',
|
| 79 |
button_primary_text_color='#FFFFFF',
|
| 80 |
button_primary_text_color_dark='#FFFFFF',
|
|
|
|
| 81 |
# Secondary Button
|
| 82 |
button_secondary_background_fill='#E5E7EB',
|
| 83 |
button_secondary_background_fill_dark='#4B5563',
|
|
|
|
| 85 |
button_secondary_background_fill_hover_dark='#374151',
|
| 86 |
button_secondary_text_color='#111827',
|
| 87 |
button_secondary_text_color_dark='#FFFFFF',
|
|
|
|
| 88 |
# Cancel Button
|
| 89 |
button_cancel_background_fill='#EF4444',
|
| 90 |
button_cancel_background_fill_dark='#B91C1C',
|
|
|
|
| 94 |
button_cancel_text_color_dark='#FFFFFF',
|
| 95 |
button_cancel_text_color_hover='#FFFFFF',
|
| 96 |
button_cancel_text_color_hover_dark='#FFFFFF',
|
|
|
|
| 97 |
# Other
|
| 98 |
border_color_accent_subdued='#A78BFA',
|
| 99 |
)
|
src/utils.py
CHANGED
|
@@ -4,18 +4,15 @@ utils.py
|
|
| 4 |
This file contains utility functions that are shared across the project.
|
| 5 |
These functions provide reusable logic to simplify code in other modules.
|
| 6 |
|
| 7 |
-
Key Features:
|
| 8 |
-
- Validates that required environment variables are set, raising meaningful errors otherwise.
|
| 9 |
-
- Provides helper functions for text validation and truncation.
|
| 10 |
-
|
| 11 |
Functions:
|
| 12 |
-
- truncate_text: Truncates a string to a specified length with ellipses.
|
| 13 |
- validate_env_var: Ensures the presence of a specific environment variable and retrieves its value.
|
| 14 |
-
- validate_prompt_length: Ensures that a prompt does not exceed the specified maximum length.
|
| 15 |
"""
|
| 16 |
|
| 17 |
# Standard Library Imports
|
| 18 |
import os
|
|
|
|
| 19 |
# Local Application Imports
|
| 20 |
from src.config import logger
|
| 21 |
|
|
@@ -104,18 +101,14 @@ def validate_prompt_length(prompt: str, max_length: int, min_length: int) -> Non
|
|
| 104 |
|
| 105 |
logger.debug(f'Prompt length being validated: {prompt_length} characters')
|
| 106 |
|
| 107 |
-
# Check if prompt is too short
|
| 108 |
if prompt_length < min_length:
|
| 109 |
raise ValueError(
|
| 110 |
f'Your prompt is too short. Please enter at least {min_length} characters. '
|
| 111 |
f'(Current length: {prompt_length})'
|
| 112 |
)
|
| 113 |
-
|
| 114 |
-
# Check if prompt exceeds max length
|
| 115 |
if prompt_length > max_length:
|
| 116 |
raise ValueError(
|
| 117 |
f'Your prompt is too long. Please limit it to {max_length} characters. '
|
| 118 |
f'(Current length: {prompt_length})'
|
| 119 |
)
|
| 120 |
-
|
| 121 |
logger.debug(f'Prompt length validation passed for prompt: {truncate_text(stripped_prompt)}')
|
|
|
|
| 4 |
This file contains utility functions that are shared across the project.
|
| 5 |
These functions provide reusable logic to simplify code in other modules.
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
Functions:
|
| 8 |
+
- truncate_text: Truncates a string to a specified length with ellipses. (used for logging)
|
| 9 |
- validate_env_var: Ensures the presence of a specific environment variable and retrieves its value.
|
| 10 |
+
- validate_prompt_length: Ensures that a prompt does not exceed the specified minimum or maximum length.
|
| 11 |
"""
|
| 12 |
|
| 13 |
# Standard Library Imports
|
| 14 |
import os
|
| 15 |
+
|
| 16 |
# Local Application Imports
|
| 17 |
from src.config import logger
|
| 18 |
|
|
|
|
| 101 |
|
| 102 |
logger.debug(f'Prompt length being validated: {prompt_length} characters')
|
| 103 |
|
|
|
|
| 104 |
if prompt_length < min_length:
|
| 105 |
raise ValueError(
|
| 106 |
f'Your prompt is too short. Please enter at least {min_length} characters. '
|
| 107 |
f'(Current length: {prompt_length})'
|
| 108 |
)
|
|
|
|
|
|
|
| 109 |
if prompt_length > max_length:
|
| 110 |
raise ValueError(
|
| 111 |
f'Your prompt is too long. Please limit it to {max_length} characters. '
|
| 112 |
f'(Current length: {prompt_length})'
|
| 113 |
)
|
|
|
|
| 114 |
logger.debug(f'Prompt length validation passed for prompt: {truncate_text(stripped_prompt)}')
|