-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend hype to support graphical user interface (#10)
* uv add --dev gradio * Add gui module * Add gui example * Access field constraints through metadata * Fix issues with undefined defaults * Use text analysis example for GUI * Update example * Add kwargs to create_gradio_component * Fix handling of list of Path values * Fix remaining issues around default handling * Configure asyncio_default_fixture_loop_scope * Add test coverage for Gradio functionality * Update README
- Loading branch information
Showing
8 changed files
with
1,512 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
This example shows how to construct a simple text analysis tool using Hype. | ||
Download `uv` to run this example: https://github.com/astral-sh/uv | ||
``` | ||
uv run examples/text_analysis.py | ||
``` | ||
""" | ||
|
||
# /// script | ||
# dependencies = [ | ||
# "hype @ git+https://github.com/mattt/hype.git", | ||
# "textstat", | ||
# ] | ||
# /// | ||
|
||
from pydantic import BaseModel, Field, field_validator | ||
from textstat import flesch_reading_ease, lexicon_count | ||
|
||
import hype | ||
|
||
|
||
class TextAnalysis(BaseModel): | ||
word_count: int = Field(description="Total number of words in text") | ||
reading_ease: float = Field( | ||
description="Flesch reading ease score (0-100, higher is easier)" | ||
) | ||
|
||
@field_validator('reading_ease') | ||
def clamp_reading_ease(cls, v: float) -> float: | ||
"""Clamp reading ease score between 0 and 100""" | ||
return max(0, min(100, v)) | ||
|
||
|
||
@hype.up | ||
def analyze( | ||
text: str = Field( | ||
description="Text to analyze", | ||
max_length=10000, | ||
), | ||
) -> TextAnalysis: | ||
"""Performs sentiment and readability analysis on text.""" | ||
|
||
word_count = lexicon_count(text) | ||
reading_ease = flesch_reading_ease(text) if word_count > 0 else 0 | ||
|
||
return TextAnalysis( | ||
word_count=word_count, | ||
reading_ease=reading_ease, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
hype.create_gradio_interface(analyze).launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from hype.gui.gradio import create_gradio_component, create_gradio_interface | ||
|
||
__all__ = ["create_gradio_component", "create_gradio_interface"] |
Oops, something went wrong.