CoolFace
Apppublic

microsoft/ChatGPT-Robotics

sourceHugging Facemitupdated 4y agoView on Hugging Face
63likes
app.py137 linesDownload Raw Back to root
1"""2ChatGPT + Robotics Gradio demo.3Author: Sai Vemprala4For details, please check out our blog post: https://aka.ms/ChatGPT-Robotics, and our paper:5https://www.microsoft.com/en-us/research/uploads/prod/2023/02/ChatGPT___Robotics.pdf6In this demo, we provide a quick way to interact with ChatGPT in robotics settings using some custom prompts.7As seen in our paper, we provide prompts for several scenarios: robot manipulation, drone navigation 8(in a simulated setting (airsim) as well as real life), and embodied AI. embodied_agent_closed_loop is an9experimental setting where observations from a scene can be described to ChatGPT as text.10Parts of the code were inspired by https://huggingface.co/spaces/VladislavMotkov/chatgpt_webui/11"""12 13import gradio as gr14from revChatGPT.V1 import Chatbot15import glob, os16 17access_token = None18 19 20def parse_text(text):21    lines = text.split("\n")22    for i, line in enumerate(lines):23        if "```" in line:24            items = line.split("`")25            if items[-1]:26                lines[i] = f'<pre><code class="{items[-1]}">'27            else:28                lines[i] = f"</code></pre>"29        else:30            if i > 0:31                lines[i] = "<br/>" + line.replace(" ", "&nbsp;")32    return "".join(lines)33 34 35def configure_chatgpt(info):36    access_token = info37    config = {}38    config.update({"access_token": access_token})39 40    global chatgpt41    chatgpt = Chatbot(config=config)42 43 44def ask(prompt):45    message = ""46    for data in chatgpt.ask(prompt):47        message = data["message"]48    return parse_text(message)49 50 51def query_chatgpt(inputs, history, message):52    history = history or []53    output = ask(inputs)54    history.append((inputs, output))55    return history, history, ""56 57 58def initialize_prompt(prompt_type, history):59    history = history or []60 61    if prompt_type:62        prompt_file = "./prompts/" + str(prompt_type) + ".txt"63 64        with open(prompt_file, "r") as f:65            prompt = f.read()66        output = ask(prompt)67        history.append(("<ORIGINAL PROMPT>", output))68 69    return history, history70 71 72def display_prompt(show, prompt_type):73    if not prompt_type:74        show = False75        return "Error - prompt not selected"76 77    else:78        if show:79            prompt_file = "./prompts/" + str(prompt_type) + ".txt"80 81            with open(prompt_file, "r") as f:82                prompt = f.read()83 84            return prompt85        else:86            return ""87 88 89with gr.Blocks() as demo:90    gr.Markdown("""<h3><center>ChatGPT + Robotics</center></h3>""")91    gr.Markdown(92        """This is a companion app to the work [ChatGPT for Robotics: Design Principles and Model Abilities](https://aka.ms/ChatGPT-Robotics).<br>93        This space allows you to work with ChatGPT to get it to generate code for robotics tasks, such as get a robot arm to manipulate objects, or have a drone fly around in a 3D world.<br>  94        See [README](https://huggingface.co/spaces/microsoft/ChatGPT-Robotics/blob/main/README.md) for detailed instructions."""95    )96 97    if not access_token:98        gr.Markdown("""<h4>Login to ChatGPT</h4>""")99        with gr.Row():100            with gr.Group():101                info = gr.Textbox(placeholder="Enter access token here (from https://chat.openai.com/api/auth/session)", label="ChatGPT Login")102                with gr.Row():103                    login = gr.Button("Login")104                    login.click(configure_chatgpt, inputs=[info])105 106    l = os.listdir("./prompts")107    li = [x.split(".")[0] for x in l]108 109    gr.Markdown("""<h4>Initial Prompt for ChatGPT</h4>""")110    prompt_type = gr.components.Dropdown(111        li,112        label="Select sample prompt",113        value=None,114        info="Choose a prompt based on the robot/scenario you're interested in (e.g. pick airsim or real_drone to start a drone scenario)",115    )116 117    show_prompt = gr.Checkbox(label="Display prompt")118    prompt_display = gr.Textbox(interactive=False, label="Prompt")119    show_prompt.change(fn=display_prompt, inputs=[show_prompt, prompt_type], outputs=prompt_display)120 121    initialize = gr.Button(value="Initialize")122 123    gr.Markdown("""<h4>Conversation</h4>""")124    chatgpt_robot = gr.Chatbot()125    message = gr.Textbox(126        placeholder="Enter query",127        label="",128        info='Talk to ChatGPT and ask it to help with specific tasks! For example, "take off and reach an altitude of five meters"',129    )130 131    state = gr.State()132 133    initialize.click(fn=initialize_prompt, inputs=[prompt_type, state], outputs=[chatgpt_robot, state])134 135    message.submit(query_chatgpt, inputs=[message, state], outputs=[chatgpt_robot, state, message])136 137    demo.launch()