-
Notifications
You must be signed in to change notification settings - Fork 45
Optimize gradio #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize gradio #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .cache import cleanup_workspace, setup_workspace | ||
| from .count_tokens import count_tokens | ||
| from .preview_file import preview_file |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import os | ||
| import uuid | ||
| import shutil | ||
| import uuid | ||
|
|
||
|
|
||
| def setup_workspace(folder): | ||
| request_id = str(uuid.uuid4()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import codecs | ||
| import os | ||
|
|
||
| import gradio as gr | ||
| import pandas as pd | ||
|
|
||
|
|
||
| def preview_file(file): | ||
| if file is None: | ||
| return gr.update(visible=False), gr.update(visible=False) | ||
|
|
||
| path = file.name | ||
| ext = os.path.splitext(path)[1].lower() | ||
|
|
||
| try: | ||
| if ext == ".csv": | ||
| df = pd.read_csv(path, nrows=10) | ||
|
||
| return gr.update(visible=False), gr.update(value=df, visible=True) | ||
| with codecs.open(path, "r", encoding="utf-8") as f: | ||
| text = f.read(5000) | ||
| if len(text) == 5000: | ||
| text += "\n\n... (truncated at 5000 chars)" | ||
|
Comment on lines
+19
to
+22
|
||
| return gr.update( | ||
| value=text, visible=True, language="json" if ext != ".txt" else None | ||
| ), gr.update(visible=False) | ||
| except Exception as e: # pylint: disable=broad-except | ||
| return gr.update( | ||
| value=f"Preview failed: {e}", visible=True, language=None | ||
| ), gr.update(visible=False) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lambda function should be extracted to a named function to improve code readability and maintainability.