ChatSherman2.0 / app.py
ShermanAI's picture
Update app.py
cd79030
raw
history blame
2.16 kB
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?"
else:
prompt = "I'm an AI chatbot named ShermanAI designed by a student at Department of Electronic and Information Engineering, The Hong Kong Polytechnic University designed to help you with your engineering 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."
examples = [
["What is the difference between a resistor and a capacitor?", []],
["Can you explain the concept of electrical conductivity?", []],
["How do you calculate the force required to move an object?", []]
]
inputs = [gr.inputs.Textbox(label="Enter your question: "), "state"]
outputs = ["chatbot", "state"]
interface = gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples)
# Add JavaScript code to reset the input textbox value on the client side
interface.script("""
function resetInput() {
document.getElementById("input-textbox").value = "";
}
""")
# Call the resetInput function when the user submits a question
interface.layout.submit_button.onclick = "resetInput();"
interface.launch(debug=True)