Spaces:
Runtime error
Runtime error
Canstralian
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,60 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
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 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
)
|
61 |
-
|
62 |
-
# Add additional features to the interface
|
63 |
-
with demo:
|
64 |
-
gr.Button("Clear Chat", elem_id="clear-chat").click(clear_chat, outputs=["chatbot", "textbox"])
|
65 |
-
|
66 |
-
if __name__ == "__main__":
|
67 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from huggingface_hub import HfApi, SpaceHardware
|
4 |
+
|
5 |
+
# Set up Hugging Face API token and Space ID
|
6 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Ensure your Hugging Face token is set as a secret
|
7 |
+
TRAINING_SPACE_ID = "your_space_id_here" # Replace with your actual space ID
|
8 |
+
|
9 |
+
# Initialize Hugging Face API
|
10 |
+
api = HfApi(token=HF_TOKEN)
|
11 |
+
|
12 |
+
# Function to check for a scheduled task (this is a placeholder for your actual task-checking logic)
|
13 |
+
def get_task():
|
14 |
+
# You can implement logic here to check for scheduled tasks
|
15 |
+
return None # For example, return None if no task is scheduled
|
16 |
+
|
17 |
+
# Function to add a new task (you can implement this depending on your use case)
|
18 |
+
def add_task(task):
|
19 |
+
# Logic to add a new task
|
20 |
+
st.write(f"Task '{task}' added!")
|
21 |
+
|
22 |
+
# Function to mark the task as "DONE" (this is a placeholder)
|
23 |
+
def mark_as_done(task):
|
24 |
+
# Mark the task as done once it's completed
|
25 |
+
st.write(f"Task '{task}' completed!")
|
26 |
+
|
27 |
+
# Function to simulate training the model (replace with actual training logic)
|
28 |
+
def train_and_upload(task):
|
29 |
+
# Implement your model training logic here
|
30 |
+
st.write(f"Training model with task: {task}")
|
31 |
+
|
32 |
+
# Check if there’s an existing task
|
33 |
+
task = get_task()
|
34 |
+
|
35 |
+
if task is None:
|
36 |
+
# Display Gradio interface to request a new task
|
37 |
+
def gradio_fn(task):
|
38 |
+
# On user request, add task and request hardware
|
39 |
+
add_task(task)
|
40 |
+
api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.T4_MEDIUM)
|
41 |
+
|
42 |
+
# Use Streamlit to request a task (Gradio interface or a simple button to simulate this)
|
43 |
+
task_input = st.text_input("Enter task name", "")
|
44 |
+
if st.button("Request Task"):
|
45 |
+
gradio_fn(task_input)
|
46 |
+
else:
|
47 |
+
# If a task is available, check for hardware
|
48 |
+
runtime = api.get_space_runtime(repo_id=TRAINING_SPACE_ID)
|
49 |
+
if runtime.hardware == SpaceHardware.T4_MEDIUM:
|
50 |
+
# Fine-tune model on GPU if available
|
51 |
+
train_and_upload(task)
|
52 |
+
|
53 |
+
# Mark task as "DONE" after training
|
54 |
+
mark_as_done(task)
|
55 |
+
|
56 |
+
# Reset to CPU hardware after training
|
57 |
+
api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.CPU_BASIC)
|
58 |
+
else:
|
59 |
+
# If GPU hardware is not available, request it
|
60 |
+
api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.T4_MEDIUM)
|
|
|
|
|
|
|
|
|
|
|
|
|
|