Sure! I can help you set up a graphical user interface (GUI) to interact with the yuntian-deng/o1mini AI using the provided API documentation. We'll create a Python-based GUI using Gradio, which is a user-friendly library for building interactive interfaces for machine learning models.
Here's a step-by-step guide to help you get started:
Table of Contents
- Prerequisites
- Installation
- Understanding the API Endpoints
- Setting Up the GUI
- Running the Application
- Complete Python Script
- Additional Enhancements (Optional)
Prerequisites
Before proceeding, ensure you have the following:
- Python 3.7 or later: You can download it from python.org.
- Basic knowledge of Python programming.
- An API token (if the Space
yuntian-deng/o1mini is private). If it's public, you might not need one.
Installation
-
Create a Virtual Environment (Optional but Recommended):
It's good practice to create a separate virtual environment for your project to manage dependencies.
python -m venv o1mini_gui_env
Activate the virtual environment:
-
Install Required Python Packages:
We'll need gradio and gradio_client as per the API documentation.
pip install gradio gradio_client
Understanding the API Endpoints
Based on the provided API documentation for yuntian-deng/o1mini, there are several endpoints:
-
/enable_inputs
- Description: Enables input components.
- Parameters: None
- Returns: 1 element
-
/reset_textbox
- Description: Resets the textbox component.
- Parameters: None
- Returns: String
-
/predict
- Description: Primary prediction endpoint.
- Parameters:
inputs (str): User input text.
top_p (float): Nucleus sampling parameter.
temperature (float): Sampling temperature.
chat_counter (float): Chat counter.
chatbot (tuple): Chat history.
- Returns:
- Updated chatbot tuple
- Updated chat counter
- Status code from OpenAI server
- Updated input textbox value
-
/reset_textbox_1
- Description: Another endpoint to reset the textbox.
- Parameters: None
- Returns: String
-
/predict_1
- Description: Secondary prediction endpoint (similar to
/predict).
- Parameters: Same as
/predict
- Returns: Same as
/predict
For simplicity, we'll focus on the /predict endpoint to handle user inputs and display AI responses.
Setting Up the GUI
We'll use Gradio to build a simple web-based GUI that allows users to:
- Enter text input.
- Adjust
top_p and temperature sliders.
- View the chat history.
- Send inputs to the AI and display responses.
Step-by-Step Guide:
-
Import Necessary Libraries:
import gradio as gr
from gradio_client import Client
-
Initialize the Client:
# If the Space is private, include your Hugging Face token
client = Client("yuntian-deng/o1mini")
# If a token is required:
# client = Client("yuntian-deng/o1mini", token="YOUR_HUGGING_FACE_TOKEN")
-
Define the Prediction Function:
This function will interact with the /predict endpoint.
def predict_ai(input_text, top_p, temperature, chat_history):
try:
# Call the /predict endpoint
result = client.predict(
inputs=input_text,
top_p=top_p,
temperature=temperature,
chat_counter=len(chat_history),
chatbot=chat_history,
api_name="/predict"
)
# Unpack the result
updated_chatbot, updated_chat_counter, status_code, updated_input = result
# Update the chat history
chat_history.append((input_text, updated_chatbot[1])) # Assuming AI response is at index 1
return chat_history, updated_chat_counter, status_code, ""
except Exception as e:
return chat_history, 0, f"Error: {str(e)}", input_text
-
Create the Gradio Interface:
with gr.Blocks() as demo:
gr.Markdown("## yuntian-deng/o1mini AI Interface")
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot(label="Chatbot")
input_text = gr.Textbox(label="Type an input and press Enter", placeholder="Hello!!")
with gr.Column():
top_p = gr.Slider(0.1, 1.0, value=1.0, label="Top-p (nucleus sampling)")
temperature = gr.Slider(0.1, 2.0, value=1.0, label="Temperature")
# Hidden state to keep track of chat history
chat_history = gr.State([])
chat_counter = gr.State(0)
send_button = gr.Button("Send")
# Define the action when send_button is clicked
send_button.click(
predict_ai,
inputs=[input_text, top_p, temperature, chat_history],
outputs=[chatbot, chat_counter, "status_code", input_text]
)
# Allow pressing Enter to send the message
input_text.submit(
send_button.click,
inputs=[input_text, top_p, temperature, chat_history],
outputs=[chatbot, chat_counter, "status_code", input_text]
)
Running the Application
-
Save the Python Script:
Create a new Python file, e.g., o1mini_gui.py, and paste the complete script outlined below.
-
Run the Script:
In your terminal or command prompt, navigate to the directory containing o1mini_gui.py and execute:
python o1mini_gui.py
-
Access the GUI:
After running the script, Gradio will provide a local URL (e.g., http://127.0.0.1:7860/). Open this URL in your web browser to interact with the AI.
Complete Python Script
Here's the complete Python script combining all the steps mentioned above:
import gradio as gr
from gradio_client import Client
# Initialize the client
client = Client("yuntian-deng/o1mini")
# If a token is required, uncomment the following line and replace YOUR_HUGGING_FACE_TOKEN
# client = Client("yuntian-deng/o1mini", token="YOUR_HUGGING_FACE_TOKEN")
def predict_ai(input_text, top_p, temperature, chat_history):
"""
Function to handle AI predictions using the /predict endpoint.
Parameters:
- input_text (str): User input.
- top_p (float): Nucleus sampling parameter.
- temperature (float): Sampling temperature.
- chat_history (list): List of tuples containing the conversation history.
Returns:
- Updated chat history.
- Updated chat counter.
- Status code or error message.
- Clears the input textbox.
"""
try:
# Call the /predict endpoint
result = client.predict(
inputs=input_text,
top_p=top_p,
temperature=temperature,
chat_counter=len(chat_history),
chatbot=chat_history,
api_name="/predict"
)
# Unpack the result
updated_chatbot, updated_chat_counter, status_code, updated_input = result
# Extract AI response (assuming it's the second element in the tuple)
if isinstance(updated_chatbot, tuple) and len(updated_chatbot) > 1:
ai_response = updated_chatbot[1]
else:
ai_response = "Received unexpected response format."
# Update the chat history
chat_history.append((input_text, ai_response))
return chat_history, updated_chat_counter, status_code, ""
except Exception as e:
return chat_history, 0, f"Error: {str(e)}", input_text
# Build the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## yuntian-deng/o1mini AI Interface")
gr.Markdown("Interact with the AI by typing your message and adjusting the parameters below.")
with gr.Row():
with gr.Column():
chatbot_display = gr.Chatbot(label="Chatbot")
input_text = gr.Textbox(label="Type an input and press Enter", placeholder="Hello!!")
with gr.Column():
top_p_slider = gr.Slider(0.1, 1.0, value=1.0, step=0.1, label="Top-p (nucleus sampling)")
temperature_slider = gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="Temperature")
send_button = gr.Button("Send")
# Hidden state to keep track of chat history
chat_history = gr.State([])
chat_counter = gr.State(0)
# Define the action when send_button is clicked
send_button.click(
predict_ai,
inputs=[input_text, top_p_slider, temperature_slider, chat_history],
outputs=[chatbot_display, chat_counter, "status_code", input_text]
)
# Allow pressing Enter to send the message
input_text.submit(
send_button.click,
inputs=[input_text, top_p_slider, temperature_slider, chat_history],
outputs=[chatbot_display, chat_counter, "status_code", input_text]
)
# Launch the interface
demo.launch()
Additional Enhancements (Optional)
Depending on your requirements, you might consider adding the following features:
-
API Token Input:
If the Space is private and requires authentication, you can add a textbox for users to input their Hugging Face token.
-
Response Status Display:
Display the status code or error messages to inform users about the request's success or failure.
-
Clear Chat History Button:
Add a button to reset the conversation history when needed.
-
Styling and Layout:
Customize the interface's appearance to make it more user-friendly and visually appealing.
Example: Adding a Clear Chat History Button
def clear_chat():
return [], 0, "", ""
clear_button = gr.Button("Clear Chat")
clear_button.click(
clear_chat,
inputs=[],
outputs=[chatbot_display, chat_counter, "status_code", input_text]
)
Place the clear_button in the desired location within the Gradio Blocks.
Troubleshooting
-
API Access Issues:
- Private Space: Ensure you're providing the correct Hugging Face token.
- Rate Limiting: If you encounter rate limits, consider slowing down the request frequency.
-
Unexpected Responses:
- Verify the structure of the API responses. You might need to adjust the
predict_ai function based on the actual response format.
-
Dependencies Errors:
- Ensure all required packages are installed correctly. You can reinstall them using
pip if necessary.
pip install --upgrade gradio gradio_client
-
Port Conflicts:
- If Gradio's default port
7860 is in use, specify a different port when launching.
demo.launch(server_port=7861)
Conclusion
By following the steps above, you should have a functional GUI that interacts with the yuntian-deng/o1mini AI model using the provided API endpoints. Gradio offers a versatile and straightforward way to build such interfaces, making it easy to customize and extend according to your needs.
Feel free to enhance the GUI further by adding more features or integrating additional API endpoints as necessary. If you encounter any issues or have further questions, don't hesitate to ask!