Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
from huggingface_hub import HfApi, SpaceHardware
|
| 4 |
|
| 5 |
# Set up Hugging Face API token and Space ID
|
|
@@ -17,44 +17,60 @@ def get_task():
|
|
| 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 |
-
|
| 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 |
-
|
| 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 |
-
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
if task is None:
|
| 36 |
-
|
| 37 |
-
|
| 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 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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
|
| 60 |
-
api.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
from huggingface_hub import HfApi, SpaceHardware
|
| 4 |
|
| 5 |
# Set up Hugging Face API token and Space ID
|
|
|
|
| 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 |
+
return 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 |
+
return 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 |
+
return f"Training model with task: {task}"
|
| 31 |
|
| 32 |
+
# Gradio function to simulate chat-like interface
|
| 33 |
+
def gradio_fn(task_input, history):
|
| 34 |
+
task = get_task()
|
| 35 |
|
| 36 |
+
if task is None:
|
| 37 |
+
# If no task, add a new task and request hardware
|
| 38 |
+
add_task_response = add_task(task_input)
|
|
|
|
|
|
|
| 39 |
api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.T4_MEDIUM)
|
| 40 |
+
|
| 41 |
+
# Add the new task response to the chat history
|
| 42 |
+
history.append(("Bot", add_task_response))
|
| 43 |
+
return "", history # Clear the input box and return updated history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
+
# If a task is available, check for hardware
|
| 46 |
+
runtime = api.get_space_runtime(repo_id=TRAINING_SPACE_ID)
|
| 47 |
+
if runtime.hardware == SpaceHardware.T4_MEDIUM:
|
| 48 |
+
# Fine-tune model on GPU if available
|
| 49 |
+
train_and_upload_response = train_and_upload(task)
|
| 50 |
+
mark_as_done_response = mark_as_done(task)
|
| 51 |
+
|
| 52 |
+
# Add responses to history
|
| 53 |
+
history.append(("Bot", train_and_upload_response))
|
| 54 |
+
history.append(("Bot", mark_as_done_response))
|
| 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)
|
| 61 |
+
history.append(("Bot", "Requesting GPU hardware..."))
|
| 62 |
+
|
| 63 |
+
return "", history # Clear the input box and return updated history
|
| 64 |
+
|
| 65 |
+
# Create the Gradio interface for chat
|
| 66 |
+
chat_interface = gr.Interface(
|
| 67 |
+
fn=gradio_fn,
|
| 68 |
+
inputs=[gr.Textbox(label="Enter task name", placeholder="Type your task here...", lines=1)],
|
| 69 |
+
outputs=[gr.Chatbot()],
|
| 70 |
+
live=True,
|
| 71 |
+
title="Task Manager Bot", # Optional: Title for the interface
|
| 72 |
+
description="Interact with the bot to manage tasks and trigger model training."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Launch the Gradio interface
|
| 76 |
+
chat_interface.launch()
|