Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
from dotenv import load_dotenv | |
import openai | |
from utils import compress | |
from google_manager.fassade import Fassade | |
from google.oauth2.credentials import Credentials | |
from description import DESCRIPTION | |
import gradio as gr | |
load_dotenv() | |
# configuring openai package | |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | |
openai.api_key = OPENAI_API_KEY | |
def load_prompt(path): | |
with open(path) as f: | |
lines = f.readlines() | |
return "".join(lines) | |
def chat(passage, max_tokens=256, temprature=0, debug=False): | |
if debug: | |
passage = """ | |
A car or automobile is a motor vehicle with wheels. Most definitions of cars say that they run primarily on roads, seat one to eight people, have four wheels, and mainly transport people (rather than goods). | |
""" | |
prompt = load_prompt("summary_prompt.txt").replace("<<SUMMARY>>", passage) | |
summary = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "user", "content": prompt}], | |
) | |
return summary["choices"][0]["message"]["content"].strip() | |
def transcribe(audio_file): | |
audio_file = open(audio_file, "rb") | |
transcription = openai.Audio.transcribe("whisper-1", audio_file, language="en") | |
transcription = transcription["text"] | |
return transcription | |
def predict(input, request: gr.Request, history=[]): | |
compress(input) | |
print("whisper starts") | |
transcription = transcribe(input) | |
print("whisper ends") | |
print("gpt starts") | |
answer = chat(transcription) | |
print("gpt ends") | |
# upload the input/answer to google drive | |
doc_content = "user:\n" f"{transcription}\n" "\n" "summary:\n" f"{answer}\n" | |
creds_dict = vars(request.session).get("credentials", {}) | |
if not creds_dict: | |
raise Exception("Credentials not found in session") | |
# Create credentials object from dictionary | |
try: | |
creds = Credentials.from_authorized_user_info(info=vars(creds_dict)) | |
except Exception as e: | |
raise Exception(f"Invalid credentials in session with the following error{e}") | |
Fassade.upload_to_drive(creds, doc_content) | |
history.append((transcription, answer)) | |
response = history | |
return response, history | |
with gr.Blocks() as Ui: | |
gr.Markdown(DESCRIPTION) | |
chatbot = gr.Chatbot() | |
state = gr.State([]) | |
with gr.Row(): | |
audio_file = gr.Audio(label="Audio", source="microphone", type="filepath") | |
audio_file.change(predict, [audio_file, state], [chatbot, state]) | |