Initial commit for Hugging Face Space
Browse files- .idea/.gitignore +3 -0
- .idea/Exercises_HuggingFace.iml +10 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +7 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- README.md +1 -1
- app.py +99 -51
.idea/.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
.idea/Exercises_HuggingFace.iml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$">
|
| 5 |
+
<excludeFolder url="file://$MODULE_DIR$/Obsidian" />
|
| 6 |
+
</content>
|
| 7 |
+
<orderEntry type="jdk" jdkName="Python 3.11 (Exercises_HuggingFace)" jdkType="Python SDK" />
|
| 8 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 9 |
+
</component>
|
| 10 |
+
</module>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/misc.xml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="Black">
|
| 4 |
+
<option name="sdkName" value="Python 3.11 (Exercises_HuggingFace)" />
|
| 5 |
+
</component>
|
| 6 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (Exercises_HuggingFace)" project-jdk-type="Python SDK" />
|
| 7 |
+
</project>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/Exercises_HuggingFace.iml" filepath="$PROJECT_DIR$/.idea/Exercises_HuggingFace.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="" vcs="Git" />
|
| 5 |
+
</component>
|
| 6 |
+
</project>
|
README.md
CHANGED
|
@@ -9,4 +9,4 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
Under construction.
|
app.py
CHANGED
|
@@ -1,64 +1,112 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 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 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Load secrets
|
| 6 |
+
CORRECT_PASSWORD = os.getenv("APP_PASSWORD") # Set this in Hugging Face Spaces secrets
|
| 7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY") # Also store securely
|
|
|
|
| 8 |
|
| 9 |
+
# -------------------------------
|
| 10 |
+
# Define Functions for App Logic
|
| 11 |
+
# -------------------------------
|
| 12 |
|
| 13 |
+
def login(password):
|
| 14 |
+
"""
|
| 15 |
+
Check the submitted password.
|
| 16 |
+
If correct, hide the login container and show the main app.
|
| 17 |
+
Otherwise, return an error message.
|
| 18 |
+
"""
|
| 19 |
+
if password == CORRECT_PASSWORD:
|
| 20 |
+
# Hide login page, show main app, and clear error message
|
| 21 |
+
return gr.update(visible=False), gr.update(visible=True), ""
|
| 22 |
+
else:
|
| 23 |
+
# Remain on login page with an error message
|
| 24 |
+
return gr.update(visible=True), gr.update(visible=False), "❌ Incorrect password. Please try again or contact Ben."
|
| 25 |
|
| 26 |
+
def diagnoser_query(user_query):
|
| 27 |
+
"""
|
| 28 |
+
Process the Diagnoser query.
|
| 29 |
+
(For now, simply pass the query to the OpenAI API.)
|
| 30 |
+
"""
|
| 31 |
+
response = openai.ChatCompletion.create(
|
| 32 |
+
model="gpt-4o-mini",
|
| 33 |
+
messages=[
|
| 34 |
+
{
|
| 35 |
+
"role": "user",
|
| 36 |
+
"content": (
|
| 37 |
+
"What is wrong with this exercise? In other words, what about it should be improved? "
|
| 38 |
+
"What stands between it and a perfect exercise that couldn't be better? Don't give solutions yet, "
|
| 39 |
+
"only a diagnosis\n "
|
| 40 |
+
f"{user_query}"
|
| 41 |
+
),
|
| 42 |
+
}
|
| 43 |
+
],
|
| 44 |
+
)
|
| 45 |
+
return response["choices"][0]["message"]["content"]
|
| 46 |
|
| 47 |
+
def distractors_query(user_query):
|
| 48 |
+
"""
|
| 49 |
+
Process the Distractors brainstorm query.
|
| 50 |
+
(For now, simply pass the query to the OpenAI API.)
|
| 51 |
+
"""
|
| 52 |
+
response = openai.ChatCompletion.create(
|
| 53 |
+
model="gpt-4o-mini",
|
| 54 |
+
messages=[{"role": "user", "content": user_query}]
|
| 55 |
+
)
|
| 56 |
+
return response["choices"][0]["message"]["content"]
|
| 57 |
|
| 58 |
+
# -------------------------------
|
| 59 |
+
# Build the Gradio Interface
|
| 60 |
+
# -------------------------------
|
| 61 |
|
| 62 |
+
with gr.Blocks() as demo:
|
| 63 |
+
# --- Login Page ---
|
| 64 |
+
with gr.Column(visible=True, elem_id="login_page") as login_container:
|
| 65 |
+
gr.Markdown("## 🔒 Please Login")
|
| 66 |
+
password_input = gr.Textbox(label="Enter Password", type="password", placeholder="Enter password to access the app")
|
| 67 |
+
login_button = gr.Button("Login")
|
| 68 |
+
login_error = gr.Markdown(value="") # To display error messages
|
|
|
|
| 69 |
|
| 70 |
+
# --- Main App (initially hidden) ---
|
| 71 |
+
with gr.Column(visible=False, elem_id="main_app") as app_container:
|
| 72 |
+
gr.Markdown("## Core Functionalities")
|
| 73 |
+
# Use gr.Tabs for persistent states between functionalities
|
| 74 |
+
with gr.Tabs():
|
| 75 |
+
with gr.TabItem("Diagnoser"):
|
| 76 |
+
gr.Markdown("### Diagnoser")
|
| 77 |
+
diagnoser_input = gr.Textbox(label="Enter Diagnoser Query", placeholder="Type your query here...")
|
| 78 |
+
diagnoser_button = gr.Button("Submit")
|
| 79 |
+
diagnoser_output = gr.Textbox(label="Response", interactive=False)
|
| 80 |
+
with gr.TabItem("Distractors brainstorm"):
|
| 81 |
+
gr.Markdown("### Distractors brainstorm")
|
| 82 |
+
distractors_input = gr.Textbox(label="Enter Brainstorm Query", placeholder="Type your query here...")
|
| 83 |
+
distractors_button = gr.Button("Submit")
|
| 84 |
+
distractors_output = gr.Textbox(label="Response", interactive=False)
|
| 85 |
|
| 86 |
+
# -------------------------------
|
| 87 |
+
# Set Up Interactions
|
| 88 |
+
# -------------------------------
|
| 89 |
|
| 90 |
+
# When the login button is clicked, check the password and update container visibility
|
| 91 |
+
login_button.click(
|
| 92 |
+
fn=login,
|
| 93 |
+
inputs=[password_input],
|
| 94 |
+
outputs=[login_container, app_container, login_error]
|
| 95 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
# Connect the Diagnoser functionality
|
| 98 |
+
diagnoser_button.click(
|
| 99 |
+
fn=diagnoser_query,
|
| 100 |
+
inputs=[diagnoser_input],
|
| 101 |
+
outputs=[diagnoser_output]
|
| 102 |
+
)
|
| 103 |
|
| 104 |
+
# Connect the Distractors brainstorm functionality
|
| 105 |
+
distractors_button.click(
|
| 106 |
+
fn=distractors_query,
|
| 107 |
+
inputs=[distractors_input],
|
| 108 |
+
outputs=[distractors_output]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Launch the app
|
| 112 |
+
demo.launch()
|