Spaces:
Sleeping
Sleeping
''' | |
import subprocess | |
subprocess.check_call(["pip", "install", "-q", "openai"]) | |
subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv"]) | |
import gradio as gr | |
from transformers import TFAutoModelForCausalLM, AutoTokenizer | |
import openai | |
from dotenv import load_dotenv | |
import os | |
load_dotenv() # load environment variables from .env file | |
api_key = os.getenv("OPENAI_API_KEY") # access the value of the OPENAI_API_KEY environment variable | |
def openai_chat(prompt): | |
if "who are you" in prompt.lower() or "your name" in prompt.lower() or "name" in prompt.lower(): | |
return "My name is ChatSherman. How can I assist you today?" | |
else: | |
prompt = "I'm an AI chatbot named ChatSherman designed by a student named ShermanAI at the Department of Electronic and Information Engineering at The Hong Kong Polytechnic University to help you with your engineering questions. Also, I can assist with a wide range of topics and questions." + prompt | |
completions = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=1024, n=1, temperature=0.5,) | |
message = completions.choices[0].text | |
return message.strip() | |
def chatbot(talk_to_chatsherman, history=[]): | |
output = openai_chat(talk_to_chatsherman) | |
history.append((talk_to_chatsherman, output)) | |
return history, history | |
title = "ChatSherman" | |
description = "This is an AI chatbot powered by ShermanAI. Enter your question below to get started." | |
examples = [ | |
["What is ChatSherman, and how does it work?", []], | |
["Is my personal information and data safe when I use the ChatSherman chatbot?", []], | |
["What are some common applications of deep learning in engineering?", []] | |
] | |
inputs = [gr.inputs.Textbox(label="Talk to ChatSherman: "), "state"] | |
outputs = ["chatbot", "state"] | |
interface = gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples) | |
interface.launch(debug=True) | |
''' | |
python -m pip install --upgrade pip | |
import subprocess | |
subprocess.check_call(["pip", "install", "-q", "openai"]) | |
subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv"]) | |
import openai | |
import gradio as gr | |
openai.api_key = "OPENAI_API_KEY" | |
def predict(message, history): | |
history_openai_format = [] | |
for human, assistant in history: | |
history_openai_format.append({"role": "user", "content": human }) | |
history_openai_format.append({"role": "assistant", "content":assistant}) | |
history_openai_format.append({"role": "user", "content": message}) | |
response = openai.ChatCompletion.create( | |
model='gpt-3.5-turbo', | |
messages= history_openai_format, | |
temperature=1.0, | |
stream=True | |
) | |
partial_message = "" | |
for chunk in response: | |
if len(chunk['choices'][0]['delta']) != 0: | |
partial_message = partial_message + chunk['choices'][0]['delta']['content'] | |
yield partial_message | |
gr.ChatInterface(predict).queue().launch() |