File size: 1,887 Bytes
833627a
fee4a07
36694ba
 
 
 
6b17378
36694ba
 
eb93dd0
36694ba
56205d5
 
 
 
 
 
 
8a82df0
82e5470
56205d5
 
 
658baf1
82e5470
56205d5
148cfd6
56205d5
 
 
148cfd6
30ab16a
cd79030
b393e78
36694ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import subprocess
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 at the Department of Electronic and Information Engineering at The Hong Kong Polytechnic University to help you with your engineering questions. I specialize in mechanical, electrical, and civil engineering, and can assist with a wide range of topics, from circuit design to structural analysis. " + 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 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)
interface.launch(debug=True)