Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,8 +13,8 @@ import openai
|
|
| 13 |
# Load environment variables
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
|
| 19 |
# Set up logging
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -30,18 +30,17 @@ def transcribe_audio(file):
|
|
| 30 |
logging.info("Transcribing audio file")
|
| 31 |
file = convert_to_supported_format(file)
|
| 32 |
logging.info("Converted file to WAV format")
|
| 33 |
-
|
| 34 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
| 35 |
temp_file.write(file.getvalue())
|
| 36 |
temp_file_path = temp_file.name
|
| 37 |
|
| 38 |
try:
|
| 39 |
-
with
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return transcript.text
|
| 45 |
except Exception as e:
|
| 46 |
logging.error(f"Error in transcription: {str(e)}")
|
| 47 |
return f"Error in transcription: {str(e)}"
|
|
@@ -63,7 +62,7 @@ def get_transcript(url):
|
|
| 63 |
|
| 64 |
def summarize_text(text):
|
| 65 |
try:
|
| 66 |
-
response =
|
| 67 |
model="gpt-3.5-turbo",
|
| 68 |
messages=[
|
| 69 |
{"role": "system", "content": "You are a helpful assistant."},
|
|
@@ -71,7 +70,7 @@ def summarize_text(text):
|
|
| 71 |
],
|
| 72 |
max_tokens=150
|
| 73 |
)
|
| 74 |
-
return response
|
| 75 |
except Exception as e:
|
| 76 |
return f"Error in summarizing text: {str(e)}"
|
| 77 |
|
|
@@ -100,3 +99,5 @@ elif option == "Upload audio/video file":
|
|
| 100 |
transcript_text = transcribe_audio(uploaded_file)
|
| 101 |
summary = handle_summary(transcript_text)
|
| 102 |
st.text_area("Summary", summary, height=200)
|
|
|
|
|
|
|
|
|
| 13 |
# Load environment variables
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
+
# Set up OpenAI API
|
| 17 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 18 |
|
| 19 |
# Set up logging
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 30 |
logging.info("Transcribing audio file")
|
| 31 |
file = convert_to_supported_format(file)
|
| 32 |
logging.info("Converted file to WAV format")
|
| 33 |
+
|
| 34 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
| 35 |
temp_file.write(file.getvalue())
|
| 36 |
temp_file_path = temp_file.name
|
| 37 |
|
| 38 |
try:
|
| 39 |
+
with warnings.catch_warnings():
|
| 40 |
+
warnings.simplefilter("ignore")
|
| 41 |
+
with open(temp_file_path, "rb") as audio_file:
|
| 42 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 43 |
+
return transcript["text"]
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
logging.error(f"Error in transcription: {str(e)}")
|
| 46 |
return f"Error in transcription: {str(e)}"
|
|
|
|
| 62 |
|
| 63 |
def summarize_text(text):
|
| 64 |
try:
|
| 65 |
+
response = openai.ChatCompletion.create(
|
| 66 |
model="gpt-3.5-turbo",
|
| 67 |
messages=[
|
| 68 |
{"role": "system", "content": "You are a helpful assistant."},
|
|
|
|
| 70 |
],
|
| 71 |
max_tokens=150
|
| 72 |
)
|
| 73 |
+
return response['choices'][0]['message']['content'].strip()
|
| 74 |
except Exception as e:
|
| 75 |
return f"Error in summarizing text: {str(e)}"
|
| 76 |
|
|
|
|
| 99 |
transcript_text = transcribe_audio(uploaded_file)
|
| 100 |
summary = handle_summary(transcript_text)
|
| 101 |
st.text_area("Summary", summary, height=200)
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
main()
|