Spaces:
Runtime error
Runtime error
Converting the interface to summarization
Browse files- app.py +35 -11
- summary_prompt.txt +5 -0
- utils.py +13 -13
app.py
CHANGED
|
@@ -2,7 +2,8 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import openai
|
| 5 |
-
|
|
|
|
| 6 |
from utils import compress
|
| 7 |
|
| 8 |
from description import DESCRIPTION
|
|
@@ -14,19 +15,42 @@ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
| 14 |
openai.api_key = OPENAI_API_KEY
|
| 15 |
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
model="gpt-3.5-turbo",
|
| 26 |
-
messages=
|
| 27 |
)
|
| 28 |
|
| 29 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
def transcribe(audio_file):
|
|
@@ -40,7 +64,7 @@ def predict(input, history=[]):
|
|
| 40 |
compress(input)
|
| 41 |
transcription = transcribe(input)
|
| 42 |
|
| 43 |
-
answer = chat(transcription
|
| 44 |
history.append((transcription, answer))
|
| 45 |
response = history
|
| 46 |
return response, history
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import openai
|
| 5 |
+
|
| 6 |
+
# from utils import serialize
|
| 7 |
from utils import compress
|
| 8 |
|
| 9 |
from description import DESCRIPTION
|
|
|
|
| 15 |
openai.api_key = OPENAI_API_KEY
|
| 16 |
|
| 17 |
|
| 18 |
+
def load_prompt(path):
|
| 19 |
+
with open(path) as f:
|
| 20 |
+
lines = f.readlines()
|
| 21 |
+
return "".join(lines)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def chat(passage, max_tokens=256, temprature=0, debug=False):
|
| 25 |
+
|
| 26 |
+
if debug:
|
| 27 |
+
passage = """
|
| 28 |
+
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).
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
prompt = load_prompt("summary_prompt.txt").replace("<<SUMMARY>>", passage)
|
| 32 |
|
| 33 |
+
summary = openai.ChatCompletion.create(
|
| 34 |
model="gpt-3.5-turbo",
|
| 35 |
+
messages=[{"role": "user", "content": prompt}],
|
| 36 |
)
|
| 37 |
|
| 38 |
+
return summary["choices"][0]["message"]["content"].strip()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# def chat(message, history):
|
| 42 |
+
# """
|
| 43 |
+
# Sends a request to the OpenAi api based on the user input and the history
|
| 44 |
+
# """
|
| 45 |
+
# messages = serialize(history)
|
| 46 |
+
# messages.append({"role": "user", "content": message})
|
| 47 |
+
|
| 48 |
+
# completion = openai.ChatCompletion.create(
|
| 49 |
+
# model="gpt-3.5-turbo",
|
| 50 |
+
# messages=messages,
|
| 51 |
+
# )
|
| 52 |
+
|
| 53 |
+
# return completion["choices"][0]["message"]["content"].strip()
|
| 54 |
|
| 55 |
|
| 56 |
def transcribe(audio_file):
|
|
|
|
| 64 |
compress(input)
|
| 65 |
transcription = transcribe(input)
|
| 66 |
|
| 67 |
+
answer = chat(transcription)
|
| 68 |
history.append((transcription, answer))
|
| 69 |
response = history
|
| 70 |
return response, history
|
summary_prompt.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a detailed summary of the following:
|
| 2 |
+
|
| 3 |
+
<<SUMMARY>>
|
| 4 |
+
|
| 5 |
+
DETAILED SUMMARY:
|
utils.py
CHANGED
|
@@ -3,22 +3,22 @@ from pathlib import Path, PurePath
|
|
| 3 |
import soundfile as sf
|
| 4 |
|
| 5 |
|
| 6 |
-
def serialize(messages):
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
|
| 24 |
def compress(audio_file):
|
|
|
|
| 3 |
import soundfile as sf
|
| 4 |
|
| 5 |
|
| 6 |
+
# def serialize(messages):
|
| 7 |
+
# """
|
| 8 |
+
# Converts a list of tuples where each element of the list represents a message to dictionary of
|
| 9 |
+
# messages
|
| 10 |
+
# """
|
| 11 |
|
| 12 |
+
# serialized_messages = []
|
| 13 |
+
# for message in messages:
|
| 14 |
+
# serialized_message = [
|
| 15 |
+
# {"role": "system", "content": message[0]},
|
| 16 |
+
# {"role": "user", "content": message[1]},
|
| 17 |
+
# ]
|
| 18 |
|
| 19 |
+
# serialized_messages.extend(serialized_message)
|
| 20 |
|
| 21 |
+
# return serialized_messages
|
| 22 |
|
| 23 |
|
| 24 |
def compress(audio_file):
|