Upload folder using huggingface_hub
Browse files- .github/workflows/update_space.yml +28 -0
- .gitignore +168 -0
- LICENSE +24 -0
- README.md +3 -9
- app.py +272 -0
- favicon.ico +0 -0
- poetry.lock +0 -0
- pyproject.toml +20 -0
- requirements.txt +63 -0
.github/workflows/update_space.yml
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Run Python script
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- master
|
| 7 |
+
|
| 8 |
+
jobs:
|
| 9 |
+
build:
|
| 10 |
+
runs-on: ubuntu-latest
|
| 11 |
+
|
| 12 |
+
steps:
|
| 13 |
+
- name: Checkout
|
| 14 |
+
uses: actions/checkout@v2
|
| 15 |
+
|
| 16 |
+
- name: Set up Python
|
| 17 |
+
uses: actions/setup-python@v2
|
| 18 |
+
with:
|
| 19 |
+
python-version: '3.9'
|
| 20 |
+
|
| 21 |
+
- name: Install Gradio
|
| 22 |
+
run: python -m pip install gradio
|
| 23 |
+
|
| 24 |
+
- name: Log in to Hugging Face
|
| 25 |
+
run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
|
| 26 |
+
|
| 27 |
+
- name: Deploy to Spaces
|
| 28 |
+
run: gradio deploy
|
.gitignore
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py,cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
# .python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# poetry
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 102 |
+
#poetry.lock
|
| 103 |
+
|
| 104 |
+
# pdm
|
| 105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 106 |
+
#pdm.lock
|
| 107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 108 |
+
# in version control.
|
| 109 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
| 110 |
+
.pdm.toml
|
| 111 |
+
.pdm-python
|
| 112 |
+
.pdm-build/
|
| 113 |
+
|
| 114 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 115 |
+
__pypackages__/
|
| 116 |
+
|
| 117 |
+
# Celery stuff
|
| 118 |
+
celerybeat-schedule
|
| 119 |
+
celerybeat.pid
|
| 120 |
+
|
| 121 |
+
# SageMath parsed files
|
| 122 |
+
*.sage.py
|
| 123 |
+
|
| 124 |
+
# Environments
|
| 125 |
+
.env
|
| 126 |
+
.venv
|
| 127 |
+
env/
|
| 128 |
+
venv/
|
| 129 |
+
ENV/
|
| 130 |
+
env.bak/
|
| 131 |
+
venv.bak/
|
| 132 |
+
|
| 133 |
+
# Spyder project settings
|
| 134 |
+
.spyderproject
|
| 135 |
+
.spyproject
|
| 136 |
+
|
| 137 |
+
# Rope project settings
|
| 138 |
+
.ropeproject
|
| 139 |
+
|
| 140 |
+
# mkdocs documentation
|
| 141 |
+
/site
|
| 142 |
+
|
| 143 |
+
# mypy
|
| 144 |
+
.mypy_cache/
|
| 145 |
+
.dmypy.json
|
| 146 |
+
dmypy.json
|
| 147 |
+
|
| 148 |
+
# Pyre type checker
|
| 149 |
+
.pyre/
|
| 150 |
+
|
| 151 |
+
# pytype static type analyzer
|
| 152 |
+
.pytype/
|
| 153 |
+
|
| 154 |
+
# Cython debug symbols
|
| 155 |
+
cython_debug/
|
| 156 |
+
|
| 157 |
+
# PyCharm
|
| 158 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 159 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 160 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 161 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 162 |
+
#.idea/
|
| 163 |
+
|
| 164 |
+
# VSCode / Misc
|
| 165 |
+
.vscode
|
| 166 |
+
.ruff_cache
|
| 167 |
+
flagged
|
| 168 |
+
*.csv
|
LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BSD 2-Clause License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2024, justin
|
| 4 |
+
|
| 5 |
+
Redistribution and use in source and binary forms, with or without
|
| 6 |
+
modification, are permitted provided that the following conditions are met:
|
| 7 |
+
|
| 8 |
+
1. Redistributions of source code must retain the above copyright notice, this
|
| 9 |
+
list of conditions and the following disclaimer.
|
| 10 |
+
|
| 11 |
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
| 12 |
+
this list of conditions and the following disclaimer in the documentation
|
| 13 |
+
and/or other materials provided with the distribution.
|
| 14 |
+
|
| 15 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 16 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 17 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 18 |
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 19 |
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 20 |
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 21 |
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 22 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 23 |
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 24 |
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: 🐨
|
| 4 |
-
colorFrom: indigo
|
| 5 |
-
colorTo: red
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.44.1
|
| 8 |
app_file: app.py
|
| 9 |
-
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ankigen
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app_file: app.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 4.44.0
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
| 6 |
+
|
| 7 |
+
class Step(BaseModel):
|
| 8 |
+
explanation: str
|
| 9 |
+
output: str
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class Subtopics(BaseModel):
|
| 13 |
+
steps: List[Step]
|
| 14 |
+
result: List[str]
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class Topics(BaseModel):
|
| 18 |
+
result: List[Subtopics]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class CardFront(BaseModel):
|
| 22 |
+
question: Optional[str] = None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class CardBack(BaseModel):
|
| 26 |
+
answer: Optional[str] = None
|
| 27 |
+
explanation: str
|
| 28 |
+
example: str
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class Card(BaseModel):
|
| 32 |
+
front: CardFront
|
| 33 |
+
back: CardBack
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class CardList(BaseModel):
|
| 37 |
+
topic: str
|
| 38 |
+
cards: List[Card]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def structured_output_completion(
|
| 42 |
+
client, model, response_format, system_prompt, user_prompt
|
| 43 |
+
):
|
| 44 |
+
try:
|
| 45 |
+
completion = client.beta.chat.completions.parse(
|
| 46 |
+
model=model,
|
| 47 |
+
messages=[
|
| 48 |
+
{"role": "system", "content": system_prompt.strip()},
|
| 49 |
+
{"role": "user", "content": user_prompt.strip()},
|
| 50 |
+
],
|
| 51 |
+
response_format=response_format,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"An error occurred during the API call: {e}")
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
if not hasattr(completion, "choices") or not completion.choices:
|
| 60 |
+
print("No choices returned in the completion.")
|
| 61 |
+
return None
|
| 62 |
+
|
| 63 |
+
first_choice = completion.choices[0]
|
| 64 |
+
if not hasattr(first_choice, "message"):
|
| 65 |
+
print("No message found in the first choice.")
|
| 66 |
+
return None
|
| 67 |
+
|
| 68 |
+
if not hasattr(first_choice.message, "parsed"):
|
| 69 |
+
print("Parsed message not available in the first choice.")
|
| 70 |
+
return None
|
| 71 |
+
|
| 72 |
+
return first_choice.message.parsed
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"An error occurred while processing the completion: {e}")
|
| 76 |
+
raise gr.Error(f"Processing error: {e}")
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def generate_cards(
|
| 80 |
+
api_key_input,
|
| 81 |
+
subject,
|
| 82 |
+
topic_number=1,
|
| 83 |
+
cards_per_topic=2,
|
| 84 |
+
preference_prompt="assume I'm a beginner",
|
| 85 |
+
):
|
| 86 |
+
"""
|
| 87 |
+
Generates flashcards for a given subject.
|
| 88 |
+
|
| 89 |
+
Parameters:
|
| 90 |
+
- subject (str): The subject to generate cards for.
|
| 91 |
+
- topic_number (int): Number of topics to generate.
|
| 92 |
+
- cards_per_topic (int): Number of cards per topic.
|
| 93 |
+
- preference_prompt (str): User preferences to consider.
|
| 94 |
+
|
| 95 |
+
Returns:
|
| 96 |
+
- List[List[str]]: A list of rows containing
|
| 97 |
+
[topic, question, answer, explanation, example].
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
gr.Info("Starting process")
|
| 101 |
+
|
| 102 |
+
if not api_key_input:
|
| 103 |
+
return gr.Error("Error: OpenAI API key is required.")
|
| 104 |
+
|
| 105 |
+
client = OpenAI(api_key=api_key_input)
|
| 106 |
+
model = "gpt-4o-mini"
|
| 107 |
+
|
| 108 |
+
all_card_lists = []
|
| 109 |
+
|
| 110 |
+
system_prompt = f"""
|
| 111 |
+
You are an expert in {subject}, assisting the user to master the topic while
|
| 112 |
+
keeping in mind the user's preferences: {preference_prompt}.
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
topic_prompt = f"""
|
| 116 |
+
Generate the top {topic_number} important subjects to know on {subject} in
|
| 117 |
+
order of ascending difficulty.
|
| 118 |
+
"""
|
| 119 |
+
|
| 120 |
+
try:
|
| 121 |
+
topics_response = structured_output_completion(
|
| 122 |
+
client, model, Topics, system_prompt, topic_prompt
|
| 123 |
+
)
|
| 124 |
+
if topics_response is None:
|
| 125 |
+
print("Failed to generate topics.")
|
| 126 |
+
return []
|
| 127 |
+
if not hasattr(topics_response, "result") or not topics_response.result:
|
| 128 |
+
print("Invalid topics response format.")
|
| 129 |
+
return []
|
| 130 |
+
topic_list = [
|
| 131 |
+
item for subtopic in topics_response.result for item in subtopic.result
|
| 132 |
+
][:topic_number]
|
| 133 |
+
except Exception as e:
|
| 134 |
+
raise gr.Error(f"Topic generation failed due to {e}")
|
| 135 |
+
|
| 136 |
+
for topic in topic_list:
|
| 137 |
+
card_prompt = f"""
|
| 138 |
+
You are to generate {cards_per_topic} cards on {subject}: "{topic}"
|
| 139 |
+
keeping in mind the user's preferences: {preference_prompt}.
|
| 140 |
+
|
| 141 |
+
Questions should cover both sample problems and concepts.
|
| 142 |
+
|
| 143 |
+
Use the explanation field to help the user understand the reason behind things
|
| 144 |
+
and maximize learning. Additionally, offer tips (performance, gotchas, etc.).
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
try:
|
| 148 |
+
cards = structured_output_completion(
|
| 149 |
+
client, model, CardList, system_prompt, card_prompt
|
| 150 |
+
)
|
| 151 |
+
if cards is None:
|
| 152 |
+
print(f"Failed to generate cards for topic '{topic}'.")
|
| 153 |
+
continue
|
| 154 |
+
if not hasattr(cards, "topic") or not hasattr(cards, "cards"):
|
| 155 |
+
print(f"Invalid card response format for topic '{topic}'.")
|
| 156 |
+
continue
|
| 157 |
+
all_card_lists.append(cards)
|
| 158 |
+
except Exception as e:
|
| 159 |
+
print(f"An error occurred while generating cards for topic '{topic}': {e}")
|
| 160 |
+
continue
|
| 161 |
+
|
| 162 |
+
flattened_data = []
|
| 163 |
+
|
| 164 |
+
for card_list_index, card_list in enumerate(all_card_lists, start=1):
|
| 165 |
+
try:
|
| 166 |
+
topic = card_list.topic
|
| 167 |
+
# Get the total number of cards in this list to determine padding
|
| 168 |
+
total_cards = len(card_list.cards)
|
| 169 |
+
# Calculate the number of digits needed for padding
|
| 170 |
+
padding = len(str(total_cards))
|
| 171 |
+
|
| 172 |
+
for card_index, card in enumerate(card_list.cards, start=1):
|
| 173 |
+
# Format the index with zero-padding
|
| 174 |
+
index = f"{card_list_index}.{card_index:0{padding}}"
|
| 175 |
+
question = card.front.question
|
| 176 |
+
answer = card.back.answer
|
| 177 |
+
explanation = card.back.explanation
|
| 178 |
+
example = card.back.example
|
| 179 |
+
row = [index, topic, question, answer, explanation, example]
|
| 180 |
+
flattened_data.append(row)
|
| 181 |
+
except Exception as e:
|
| 182 |
+
print(f"An error occurred while processing card {index}: {e}")
|
| 183 |
+
continue
|
| 184 |
+
|
| 185 |
+
return flattened_data
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def export_csv(d):
|
| 189 |
+
MIN_ROWS = 2
|
| 190 |
+
|
| 191 |
+
if len(d) < MIN_ROWS:
|
| 192 |
+
gr.Warning(f"The dataframe has fewer than {MIN_ROWS} rows. Nothing to export.")
|
| 193 |
+
return None
|
| 194 |
+
|
| 195 |
+
gr.Info("Exporting...")
|
| 196 |
+
d.to_csv("anki_deck.csv", index=False)
|
| 197 |
+
return gr.File(value="anki_deck.csv", visible=True)
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
with gr.Blocks(
|
| 201 |
+
gr.themes.Soft(), title="AnkiGen", css="footer{display:none !important}"
|
| 202 |
+
) as ankigen:
|
| 203 |
+
gr.Markdown("# 📚 AnkiGen - Anki Card Generator")
|
| 204 |
+
gr.Markdown(
|
| 205 |
+
"""
|
| 206 |
+
#### Generate an Anki comptible .csv using LLMs based on your subject and preferences.
|
| 207 |
+
"""
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
with gr.Row():
|
| 211 |
+
with gr.Column(scale=1):
|
| 212 |
+
gr.Markdown("### Configuration")
|
| 213 |
+
|
| 214 |
+
api_key_input = gr.Textbox(
|
| 215 |
+
label="OpenAI API Key",
|
| 216 |
+
type="password",
|
| 217 |
+
placeholder="Enter your OpenAI API key",
|
| 218 |
+
)
|
| 219 |
+
subject = gr.Textbox(
|
| 220 |
+
label="Subject",
|
| 221 |
+
placeholder="Enter the subject, e.g., 'Basic SQL Concepts'",
|
| 222 |
+
)
|
| 223 |
+
topic_number = gr.Slider(
|
| 224 |
+
label="Number of Topics", minimum=2, maximum=20, step=1, value=2
|
| 225 |
+
)
|
| 226 |
+
cards_per_topic = gr.Slider(
|
| 227 |
+
label="Cards per Topic", minimum=2, maximum=30, step=1, value=3
|
| 228 |
+
)
|
| 229 |
+
preference_prompt = gr.Textbox(
|
| 230 |
+
label="Preference Prompt",
|
| 231 |
+
placeholder="Any preferences? e.g., 'Assume I'm a beginner'",
|
| 232 |
+
)
|
| 233 |
+
generate_button = gr.Button("Generate Cards")
|
| 234 |
+
with gr.Column(scale=2):
|
| 235 |
+
gr.Markdown("### Generated Cards")
|
| 236 |
+
gr.Markdown(
|
| 237 |
+
"""
|
| 238 |
+
Subject to change: currently exports a .csv with the following fields, you can
|
| 239 |
+
create a new note type with these fields to handle importing.:
|
| 240 |
+
<b>Index, Topic, Question, Answer, Explanation, Example</b>
|
| 241 |
+
""")
|
| 242 |
+
output = gr.Dataframe(
|
| 243 |
+
headers=[
|
| 244 |
+
"Index",
|
| 245 |
+
"Topic",
|
| 246 |
+
"Question",
|
| 247 |
+
"Answer",
|
| 248 |
+
"Explanation",
|
| 249 |
+
"Example",
|
| 250 |
+
],
|
| 251 |
+
interactive=False,
|
| 252 |
+
height=800,
|
| 253 |
+
)
|
| 254 |
+
export_button = gr.Button("Export to CSV")
|
| 255 |
+
download_link = gr.File(interactive=False, visible=False)
|
| 256 |
+
|
| 257 |
+
generate_button.click(
|
| 258 |
+
fn=generate_cards,
|
| 259 |
+
inputs=[
|
| 260 |
+
api_key_input,
|
| 261 |
+
subject,
|
| 262 |
+
topic_number,
|
| 263 |
+
cards_per_topic,
|
| 264 |
+
preference_prompt,
|
| 265 |
+
],
|
| 266 |
+
outputs=output,
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
export_button.click(fn=export_csv, inputs=output, outputs=download_link)
|
| 270 |
+
|
| 271 |
+
if __name__ == "__main__":
|
| 272 |
+
ankigen.launch(share=False, favicon_path="./favicon.ico")
|
favicon.ico
ADDED
|
|
poetry.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pyproject.toml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[tool.poetry]
|
| 2 |
+
name = "ankigen"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = ""
|
| 5 |
+
authors = ["Justin <9146678+brickfrog@users.noreply.github.com>"]
|
| 6 |
+
readme = "README.md"
|
| 7 |
+
|
| 8 |
+
[tool.poetry.dependencies]
|
| 9 |
+
python = "^3.12"
|
| 10 |
+
polars = "^1.0.0"
|
| 11 |
+
openai = "^1.35.10"
|
| 12 |
+
python-dotenv = "^1.0.1"
|
| 13 |
+
gradio = "^4.44.0"
|
| 14 |
+
|
| 15 |
+
[tool.poetry.group.dev.dependencies]
|
| 16 |
+
ipykernel = "^6.29.5"
|
| 17 |
+
|
| 18 |
+
[build-system]
|
| 19 |
+
requires = ["poetry-core"]
|
| 20 |
+
build-backend = "poetry.core.masonry.api"
|
requirements.txt
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 2 |
+
annotated-types==0.7.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 3 |
+
anyio==4.6.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 4 |
+
certifi==2024.8.30 ; python_version >= "3.12" and python_version < "4.0"
|
| 5 |
+
charset-normalizer==3.3.2 ; python_version >= "3.12" and python_version < "4.0"
|
| 6 |
+
click==8.1.7 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
|
| 7 |
+
colorama==0.4.6 ; python_version >= "3.12" and python_version < "4.0" and platform_system == "Windows"
|
| 8 |
+
contourpy==1.3.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 9 |
+
cycler==0.12.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 10 |
+
distro==1.9.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 11 |
+
fastapi==0.115.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 12 |
+
ffmpy==0.4.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 13 |
+
filelock==3.16.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 14 |
+
fonttools==4.53.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 15 |
+
fsspec==2024.9.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 16 |
+
gradio-client==1.3.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 17 |
+
gradio==4.44.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 18 |
+
h11==0.14.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 19 |
+
httpcore==1.0.5 ; python_version >= "3.12" and python_version < "4.0"
|
| 20 |
+
httpx==0.27.2 ; python_version >= "3.12" and python_version < "4.0"
|
| 21 |
+
huggingface-hub==0.25.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 22 |
+
idna==3.10 ; python_version >= "3.12" and python_version < "4.0"
|
| 23 |
+
importlib-resources==6.4.5 ; python_version >= "3.12" and python_version < "4.0"
|
| 24 |
+
jinja2==3.1.4 ; python_version >= "3.12" and python_version < "4.0"
|
| 25 |
+
jiter==0.5.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 26 |
+
kiwisolver==1.4.7 ; python_version >= "3.12" and python_version < "4.0"
|
| 27 |
+
markdown-it-py==3.0.0 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
|
| 28 |
+
markupsafe==2.1.5 ; python_version >= "3.12" and python_version < "4.0"
|
| 29 |
+
matplotlib==3.9.2 ; python_version >= "3.12" and python_version < "4.0"
|
| 30 |
+
mdurl==0.1.2 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
|
| 31 |
+
numpy==2.1.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 32 |
+
openai==1.47.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 33 |
+
orjson==3.10.7 ; python_version >= "3.12" and python_version < "4.0"
|
| 34 |
+
packaging==24.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 35 |
+
pandas==2.2.3 ; python_version >= "3.12" and python_version < "4.0"
|
| 36 |
+
pillow==10.4.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 37 |
+
polars==1.7.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 38 |
+
pydantic-core==2.23.4 ; python_version >= "3.12" and python_version < "4.0"
|
| 39 |
+
pydantic==2.9.2 ; python_version >= "3.12" and python_version < "4.0"
|
| 40 |
+
pydub==0.25.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 41 |
+
pygments==2.18.0 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
|
| 42 |
+
pyparsing==3.1.4 ; python_version >= "3.12" and python_version < "4.0"
|
| 43 |
+
python-dateutil==2.9.0.post0 ; python_version >= "3.12" and python_version < "4.0"
|
| 44 |
+
python-dotenv==1.0.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 45 |
+
python-multipart==0.0.10 ; python_version >= "3.12" and python_version < "4.0"
|
| 46 |
+
pytz==2024.2 ; python_version >= "3.12" and python_version < "4.0"
|
| 47 |
+
pyyaml==6.0.2 ; python_version >= "3.12" and python_version < "4.0"
|
| 48 |
+
requests==2.32.3 ; python_version >= "3.12" and python_version < "4.0"
|
| 49 |
+
rich==13.8.1 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
|
| 50 |
+
ruff==0.6.7 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
|
| 51 |
+
semantic-version==2.10.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 52 |
+
shellingham==1.5.4 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
|
| 53 |
+
six==1.16.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 54 |
+
sniffio==1.3.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 55 |
+
starlette==0.38.6 ; python_version >= "3.12" and python_version < "4.0"
|
| 56 |
+
tomlkit==0.12.0 ; python_version >= "3.12" and python_version < "4.0"
|
| 57 |
+
tqdm==4.66.5 ; python_version >= "3.12" and python_version < "4.0"
|
| 58 |
+
typer==0.12.5 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
|
| 59 |
+
typing-extensions==4.12.2 ; python_version >= "3.12" and python_version < "4.0"
|
| 60 |
+
tzdata==2024.1 ; python_version >= "3.12" and python_version < "4.0"
|
| 61 |
+
urllib3==2.2.3 ; python_version >= "3.12" and python_version < "4.0"
|
| 62 |
+
uvicorn==0.30.6 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
|
| 63 |
+
websockets==12.0 ; python_version >= "3.12" and python_version < "4.0"
|