shreyasiv commited on
Commit
1025165
1 Parent(s): 0939b3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -35
app.py CHANGED
@@ -6,10 +6,11 @@ from pathlib import Path
6
  import shutil
7
  import whisper
8
  from dotenv import load_dotenv
9
- from zipfile import ZipFile
10
 
11
  load_dotenv()
12
 
 
13
  openai.api_key = os.getenv("OPENAI_API_KEY")
14
 
15
  @st.cache_data
@@ -41,16 +42,28 @@ def audio_to_transcript(audio_file):
41
 
42
  def text_to_news_article(text):
43
  response = openai.Completion.create(
44
- model="text-davinci-003",
45
- prompt="Write a news article in 500 words from the below text:\n"+text,
46
- temperature=0.7,
47
- max_tokens=600,
48
- top_p=1,
49
- frequency_penalty=0,
50
- presence_penalty=0
51
  )
52
  return response['choices'][0]['text']
53
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  st.markdown('# Generate and Chat with Transcripts')
56
 
@@ -62,30 +75,42 @@ if st.checkbox('Start Analysis'):
62
  video_title, audio_filename = save_audio(url_link)
63
  st.audio(audio_filename)
64
  transcript = audio_to_transcript(audio_filename)
65
- st.header("Transcript")
66
- st.success(transcript)
67
- st.header("News Article")
68
- result = text_to_news_article(transcript)
69
- st.success(result)
70
-
71
- #save the files
72
- transcript_txt = open('transcript.txt', 'w')
73
- transcript_txt.write(transcript)
74
- transcript_txt.close()
75
-
76
- article_txt = open('article.txt', 'w')
77
- article_txt.write(result)
78
- article_txt.close()
79
-
80
- zip_file = ZipFile('output.zip', 'w')
81
- zip_file.write('transcript.txt')
82
- zip_file.write('article.txt')
83
- zip_file.close()
84
-
85
- with open("output.zip", "rb") as zip_download:
86
- btn = st.download_button(
87
- label="Download ZIP",
88
- data=zip_download,
89
- file_name="output.zip",
90
- mime="application/zip"
91
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import shutil
7
  import whisper
8
  from dotenv import load_dotenv
9
+ from zipfile import ZipFile
10
 
11
  load_dotenv()
12
 
13
+ # Load OpenAI API key from .env
14
  openai.api_key = os.getenv("OPENAI_API_KEY")
15
 
16
  @st.cache_data
 
42
 
43
  def text_to_news_article(text):
44
  response = openai.Completion.create(
45
+ model="text-davinci-003",
46
+ prompt="Write a news article in 500 words from the below text:\n" + text,
47
+ temperature=0.7,
48
+ max_tokens=600,
49
+ top_p=1,
50
+ frequency_penalty=0,
51
+ presence_penalty=0
52
  )
53
  return response['choices'][0]['text']
54
 
55
+ def answer_question(question, context):
56
+ response = openai.Completion.create(
57
+ engine="text-davinci-003",
58
+ prompt=f"Q: {question}\nContext: {context}\nAnswer:",
59
+ temperature=0.7,
60
+ max_tokens=150,
61
+ top_p=1,
62
+ frequency_penalty=0,
63
+ presence_penalty=0,
64
+ )
65
+ answer = response.choices[0].text
66
+ return answer
67
 
68
  st.markdown('# Generate and Chat with Transcripts')
69
 
 
75
  video_title, audio_filename = save_audio(url_link)
76
  st.audio(audio_filename)
77
  transcript = audio_to_transcript(audio_filename)
78
+ if not transcript:
79
+ st.error("Transcript generation failed. Please try another video or check the input.")
80
+ else:
81
+ st.header("Transcript")
82
+ st.success(transcript)
83
+ st.header("News Article")
84
+ result = text_to_news_article(transcript)
85
+ st.success(result)
86
+
87
+ # Save the files
88
+ transcript_txt = open('transcript.txt', 'w')
89
+ transcript_txt.write(transcript)
90
+ transcript_txt.close()
91
+
92
+ article_txt = open('article.txt', 'w')
93
+ article_txt.write(result)
94
+ article_txt.close()
95
+
96
+ zip_file = ZipFile('output.zip', 'w')
97
+ zip_file.write('transcript.txt')
98
+ zip_file.write('article.txt')
99
+ zip_file.close()
100
+
101
+ with open("output.zip", "rb") as zip_download:
102
+ btn = st.download_button(
103
+ label="Download ZIP",
104
+ data=zip_download,
105
+ file_name="output.zip",
106
+ mime="application/zip"
107
+ )
108
+
109
+ st.header("Ask Questions")
110
+ user_question = st.text_input("Ask a question:")
111
+ if st.button("Get Answer"):
112
+ if transcript:
113
+ answer = answer_question(user_question, transcript)
114
+ st.success("Answer: " + answer)
115
+ else:
116
+ st.error("No transcript available. Please analyze a video first.")