Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,14 @@ from selenium.webdriver.chrome.options import Options
|
|
7 |
import openai
|
8 |
import gradio as gr
|
9 |
import time
|
|
|
10 |
|
11 |
-
#
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def extract_video_id(url):
|
15 |
if "youtu.be" in url: # Mobile YouTube URL
|
@@ -47,7 +52,6 @@ def get_redirected_url_video_id(mobile_url):
|
|
47 |
video_id = re.search(r"v=([^&]+)", current_url).group(1)
|
48 |
return video_id
|
49 |
|
50 |
-
|
51 |
def get_youtube_comments(video_id):
|
52 |
youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
|
53 |
comments = []
|
@@ -91,7 +95,6 @@ def get_youtube_comments(video_id):
|
|
91 |
comments = sorted(comments, key=lambda x: x['like_count'], reverse=True)[:20]
|
92 |
return [comment['text'] for comment in comments]
|
93 |
|
94 |
-
|
95 |
def generate_story(comments, temperature=0.7):
|
96 |
words = []
|
97 |
word_count = 0
|
@@ -104,25 +107,27 @@ def generate_story(comments, temperature=0.7):
|
|
104 |
word_count += len(comment_words)
|
105 |
|
106 |
comments_text = " ".join(words)
|
107 |
-
|
108 |
-
|
|
|
109 |
model="gpt-3.5-turbo",
|
110 |
-
messages=[
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
126 |
|
127 |
# Main function to execute the process
|
128 |
def main(youtube_url, temperature):
|
@@ -131,7 +136,6 @@ def main(youtube_url, temperature):
|
|
131 |
story = generate_story(comments, temperature)
|
132 |
return story
|
133 |
|
134 |
-
|
135 |
# Create Gradio interface
|
136 |
youtube_url_input = gr.Textbox(label="YouTube URL")
|
137 |
temperature_input = gr.Slider(minimum=0.0, maximum=2.0, value=0.7, label="Temperature (creativity)")
|
@@ -145,4 +149,4 @@ iface = gr.Interface(
|
|
145 |
)
|
146 |
|
147 |
# Launch the interface
|
148 |
-
iface.launch()
|
|
|
7 |
import openai
|
8 |
import gradio as gr
|
9 |
import time
|
10 |
+
from dotenv import load_dotenv
|
11 |
|
12 |
+
# Load environment variables from .env file
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
# Get API keys from environment variables
|
16 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
17 |
+
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
18 |
|
19 |
def extract_video_id(url):
|
20 |
if "youtu.be" in url: # Mobile YouTube URL
|
|
|
52 |
video_id = re.search(r"v=([^&]+)", current_url).group(1)
|
53 |
return video_id
|
54 |
|
|
|
55 |
def get_youtube_comments(video_id):
|
56 |
youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
|
57 |
comments = []
|
|
|
95 |
comments = sorted(comments, key=lambda x: x['like_count'], reverse=True)[:20]
|
96 |
return [comment['text'] for comment in comments]
|
97 |
|
|
|
98 |
def generate_story(comments, temperature=0.7):
|
99 |
words = []
|
100 |
word_count = 0
|
|
|
107 |
word_count += len(comment_words)
|
108 |
|
109 |
comments_text = " ".join(words)
|
110 |
+
|
111 |
+
openai.api_key = OPENAI_API_KEY
|
112 |
+
completion = openai.ChatCompletion.create(
|
113 |
model="gpt-3.5-turbo",
|
114 |
+
messages=[
|
115 |
+
{"role": "system", "content": f"""
|
116 |
+
Read all the comments, understand the emotions people are feeling and pick any random emotion
|
117 |
+
and create a story in first person (the person can be randomly young or old and the story can be
|
118 |
+
based in past or future) based on that emotion picking a random character keep the words
|
119 |
+
simple and a bit profound but overall simple words. Give more weight to the comments that
|
120 |
+
come earlier in sequence. The comments are given here: {comments_text}"""},
|
121 |
+
{"role": "user", "content": """
|
122 |
+
ignore the comment which has lyrics of the song,
|
123 |
+
ignore all comments similar to 'anyone in 2024', Keep the story randomly between 50-120 words.
|
124 |
+
don't mention the emotion chosen just start the story.
|
125 |
+
sometimes include bits where you tell how this song makes you feel. be very nostalgic about a feeling or a place this
|
126 |
+
takes you to. half the times end the story with a hopeful future or a dark end or humorous.. choose randomly"""}
|
127 |
+
],
|
128 |
+
temperature=temperature
|
129 |
+
)
|
130 |
+
return completion.choices[0].message['content']
|
131 |
|
132 |
# Main function to execute the process
|
133 |
def main(youtube_url, temperature):
|
|
|
136 |
story = generate_story(comments, temperature)
|
137 |
return story
|
138 |
|
|
|
139 |
# Create Gradio interface
|
140 |
youtube_url_input = gr.Textbox(label="YouTube URL")
|
141 |
temperature_input = gr.Slider(minimum=0.0, maximum=2.0, value=0.7, label="Temperature (creativity)")
|
|
|
149 |
)
|
150 |
|
151 |
# Launch the interface
|
152 |
+
iface.launch(share=True)
|