Team Ai
Apppublic

Knows-Nothing/Totally_Not_StackExchange

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py69 linesDownload Raw Back to root
1import gradio as gr2from transformers import pipeline3 4GPT = pipeline('text-generation', model='Knows-Nothing/GPT_2_FineTuned')5 6def check_if_arabic(text):7  for ch in text:8    if ('\u0600' <= ch <= '\u06FF' or9    '\u0750' <= ch <= '\u077F' or10    '\u08A0' <= ch <= '\u08FF' or11    '\uFB50' <= ch <= '\uFDFF' or12    '\uFE70' <= ch <= '\uFEFF' or13    '\U00010E60' <= ch <= '\U00010E7F' or14    '\U0001EE00' <= ch <= '\U0001EEFF'):15      return True16  return False17 18 19def chat(message, history):20    history = history or []21    message = message.lower()22    if "name" in message.split(' ') and "your" in message.split(' ') or "ur" in message.split(' '):23        response = "My name is 'Totally Not Stack Exchange'."24    elif message.endswith("?"):25        response = generate(message)26    elif "who" in message.split(' ') and "made" in message.split(' ') or "created" in message.split(' '):27        response = "Gehan Sherif and Omar Emad Created Me."28    elif "why" in message.split(' ') and "made" in message.split(' ') or "created" in message.split(' '):29        response = "Sigh, I was created as the final NLP project in CIE 553."30    elif ("good" in message.split(' ') or "fine" in message.split(' ')) and ("iam" in message.split(' ') or "am" in message.split(' ')) or ("good" in message.split(' ') or "fine" in message.split(' ')):31        response = "Thats nice to hear!"32    elif "hey" in message.split(' ') or "hi" in message.split(' ')or "yo" in message.split(' '):33        response = "Hey, how are you?"34    elif check_if_arabic(message):35        response = "بقولك ايه يا شبح اتكلم انجليزي انا من ايجيبت متتعبنيش معاك"36    else:37        response = "I don't know."38    history.append((message, response))39    return history, history40 41def generate(text):42    text+=':'43    result = GPT(text,44                  max_length=50,45                  num_return_sequences=1,46                  temperature=0.7,47                  top_k=200,48                  top_p=0.85,49                  repetition_penalty = 1.5,50                  no_repeat_ngram_size = 5,51                 )52    return result[0]['generated_text'].split(':')[1].split('.')[0]+"."53 54#Custom css for components55baseCss = '{width: fit-content; align-self: center;}'56botCss =  '{width: 505px;}'57txtCss =  '{width: 505px;}'58btnCss =  '{color: orange; width: 505px;}'59myCss =   "#row {} #btn {} #bot {} #txtbox {}".format(baseCss, btnCss, botCss, txtCss)60 61with gr.Blocks(css=myCss) as demo:62  with gr.Row(elem_id="row"):63    with gr.Column():64      chatbot = gr.Chatbot(elem_id="bot").style(color_map=("green", "blue"))65      msg = gr.Textbox(placeholder="hey", label="Talk To Totally Not Stack Exchange, to ask a question about life hacks end it with a '?'.", elem_id="txtbox")66      Send_Btn = gr.Button("Send", elem_id="btn")67      Send_Btn.click(chat, inputs=[msg,chatbot], outputs=[chatbot,chatbot])68 69demo.launch()