Last active
September 23, 2023 13:26
-
-
Save hzhangxyz/02100ec581fe91a3d98897bfdf1f818d to your computer and use it in GitHub Desktop.
gpt-3.5-instruct-webui.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import openai | |
import asyncio | |
import gradio as gr | |
MODEL = "gpt-3.5-turbo-instruct" | |
if "OPENAI_PROXY" in os.environ: | |
openai.proxy = os.environ["OPENAI_PROXY"] | |
openai.api_key = os.environ["OPENAI_API_KEY"] | |
async def complete_with_gpt(text, stop): | |
result = text | |
yield result, stop | |
stream = await openai.Completion.acreate(model=MODEL, prompt=text, stop=stop if stop else None, max_tokens=256, stream=True) | |
async for response in stream: | |
update = response["choices"][0]["text"] | |
result = result + update | |
yield result, stop | |
yield result + stop, stop | |
if __name__ == "__main__": | |
with gr.Blocks() as demo: | |
stopper = gr.Textbox(placeholder="Stop...") | |
textbox = gr.Textbox(placeholder="Prompt...", lines=32, autofocus=True) | |
btn = gr.Button("Submit") | |
btn.click(lambda x: x, textbox, textbox, show_progress='hidden').then(complete_with_gpt, [textbox, stopper], [textbox, stopper]) | |
demo.queue().launch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment