import gradio as gr
from main import index, run
from gtts import gTTS
import os, time
from transformers import pipeline
p = pipeline("automatic-speech-recognition")
"""Use text to call chat method from main.py"""
models = ["GPT-3.5", "Flan UL2", "GPT-4", "Flan T5"]
def add_text(history, text, model):
print("Question asked: " + text)
response = run_model(text, model)
history = history + [(text, response)]
print(history)
return history, ""
def run_model(text, model):
start_time = time.time()
print("start time:" + str(start_time))
response = run(text, model)
end_time = time.time()
# If response contains string `SOURCES:`, then add a \n before `SOURCES`
if "SOURCES:" in response:
response = response.replace("SOURCES:", "\nSOURCES:")
# response = response + "\n\n" + "Time taken: " + str(end_time - start_time)
print(response)
print("Time taken: " + str(end_time - start_time))
return response
def get_output(history, audio, model):
txt = p(audio)["text"]
# history.append(( (audio, ) , txt))
audio_path = 'response.wav'
response = run_model(txt, model)
# Remove all text from SOURCES: to the end of the string
trimmed_response = response.split("SOURCES:")[0]
myobj = gTTS(text=trimmed_response, lang='en', slow=False)
myobj.save(audio_path)
# split audio by / and keep the last element
# audio = audio.split("/")[-1]
# audio = audio + ".wav"
history.append(( (audio, ) , (audio_path, )))
print(history)
return history
def set_model(history, model):
print("Model selected: " + model)
history = get_first_message(history)
index(model)
return history
def get_first_message(history):
history = [(None,
'''Hi!! I AM GRADY!! I am a grading assistant to help you grade assignments based on a rubric!!
Today, I will be grading Paediatric Orthopaedic Quiz.
Use the format as given in the example below to get an accurate grade.
WARNING! I might get things wrong, so double check before your final grading. All the best. ''')]
return history
def bot(history):
return history
with gr.Blocks() as demo:
gr.HTML("