LyricGen / main copy.py
thejagstudio's picture
Upload 6 files
0373222 verified
import os
import io
from flask import Flask, request, render_template, jsonify
import google.generativeai as genai
from werkzeug.utils import secure_filename
import requests
import time
from google.generativeai import types
# Initialize Flask app
app = Flask(__name__)
# Configure Gemini API
genai.configure(api_key="AIzaSyCg9NGsLygb0sVKpviMkgV4eMPLd9nXW7w")
# Set up generation config
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 40,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
# Initialize the model
model = genai.GenerativeModel(
model_name="gemini-2.0-flash",
generation_config=generation_config,
)
def upload_to_gemini(file_bytes, mime_type=None):
"""Uploads the given file bytes to Gemini."""
file = genai.upload_file(file_bytes, mime_type=mime_type)
print(f"Uploaded file as: {file.uri}")
return file
@app.route('/')
def index():
return render_template('index.html')
@app.route('/convert', methods=['POST'])
def convert_audio():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
try:
# Read file bytes directly from the request
file_bytes = io.BytesIO(file.read())
# Determine mime type based on file extension
mime_type = 'audio/mpeg' if file.filename.endswith('.mp3') else \
'audio/wav' if file.filename.endswith('.wav') else \
'audio/x-m4a' if file.filename.endswith('.m4a') else \
'audio/ogg' if file.filename.endswith('.ogg') else None
# Upload to Gemini
gemini_file = upload_to_gemini(file_bytes, mime_type=mime_type)
# Start chat session with the uploaded file
chat_session = model.start_chat(
history=[
{
"role": "user",
"parts": [gemini_file],
},
]
)
# Generate lyrics
response = chat_session.send_message('''Extract the Gujarati lyrics from the provided audio file and output them using the following HTML template. Ensure that each line of the lyrics is correctly inserted into the template and that any placeholders are replaced with the appropriate content from the file.
```
<html>
<head>
<title>Bhaktisudha</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<link href="/static/simple.css" rel="stylesheet" type="text/css" />
<style>
</style>
</head>
<body>
<div class="main">
<div class="gtitlev3">
{Title of the file}
</div>
<div class="gpara">
Line1 <br/>
Line2 <br/>
</div>
<div class="chend">
*****
</div>
</div>
</body>
</html>
```''')
lyrics = response.text.replace("```html","").replace("```","")
return jsonify({'lyrics': lyrics})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/convert-youtube', methods=['POST'])
def convert_youtube():
try:
data = request.get_json()
url = data.get('url')
if not url:
return jsonify({'error': 'No URL provided'}), 400
# Download audio from YouTube
audio_data = download_from_youtube(url)
# Create a file-like object from the audio data
file_bytes = io.BytesIO(audio_data)
# Upload to Gemini
gemini_file = upload_to_gemini(file_bytes, mime_type='audio/mpeg')
# Start chat session with the uploaded file
chat_session = model.start_chat(
history=[
{
"role": "user",
"parts": [gemini_file],
},
]
)
# Generate lyrics
response = chat_session.send_message('''Extract the Gujarati lyrics from the provided audio file and output them using the following HTML template. Ensure that each line of the lyrics is correctly inserted into the template and that any placeholders are replaced with the appropriate content from the file.
```
<html>
<head>
<title>Bhaktisudha</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<link href="/static/simple.css" rel="stylesheet" type="text/css" />
<style>
</style>
</head>
<body>
<div class="main">
<div class="gtitlev3">
{Title of the file}
</div>
<div class="gpara">
Line1 <br/>
Line2 <br/>
</div>
<div class="chend">
*****
</div>
</div>
</body>
</html>
```''')
lyrics = response.text.replace("```html","").replace("```","")
return jsonify({'lyrics': lyrics})
except Exception as e:
return jsonify({'error': str(e)})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=7860)