133 lines
3.9 KiB
Python
133 lines
3.9 KiB
Python
from flask import Flask, request, jsonify
|
|
from dotenv import load_dotenv
|
|
load_dotenv()
|
|
from langchain.chat_models import ChatOpenAI
|
|
from langchain.schema import HumanMessage, SystemMessage
|
|
from translate import translate, models
|
|
from tldw import GetVideo
|
|
import os
|
|
|
|
app = Flask(__name__)
|
|
# set fixed port for app
|
|
app.config['SERVER_NAME'] = 'localhost:5000'
|
|
|
|
@app.route('/openai', methods=['POST'])
|
|
def openai():
|
|
content_type = request.headers.get('Content-Type')
|
|
prompt = None
|
|
temperature = 0.9
|
|
model_name = 'gpt-3.5-turbo'
|
|
if content_type == 'application/json':
|
|
json_payload = request.json
|
|
prompt = json_payload['prompt']
|
|
else:
|
|
return jsonify({'error': 'Invalid content type'}), 400
|
|
|
|
llm = ChatOpenAI(temperature = temperature, model_name = model_name)
|
|
resp = llm([HumanMessage(content=prompt)])
|
|
|
|
return {
|
|
'statusCode': 200,
|
|
'body': resp.content
|
|
}
|
|
|
|
@app.route('/tldw', methods=['POST'])
|
|
def tldw():
|
|
content_type = request.headers.get('Content-Type')
|
|
if content_type == 'application/json':
|
|
json_payload = request.json
|
|
url = json_payload['url']
|
|
else:
|
|
return jsonify({'error': 'Invalid content type'}), 400
|
|
video = GetVideo(url)
|
|
print("received request for " + url )
|
|
if not video.ytgenerated:
|
|
return {
|
|
'statusCode': 200,
|
|
'body': {
|
|
'url': video.url,
|
|
'creator': video.creator,
|
|
'title': video.title,
|
|
'transcript': video.transcript,
|
|
'summary': 'This video doesn\'t have available subtitles :-/'
|
|
}
|
|
}
|
|
temperature = 0.9
|
|
model_name = 'gpt-3.5-turbo'
|
|
summary = ""
|
|
llm = ChatOpenAI(temperature = temperature, model_name = model_name)
|
|
|
|
system = "You are a professional note taker. User will provide a part of the transcript of a Youtube video. You will reply by listing bullet points with 1 simple sentence each time."
|
|
|
|
for part in video.parts:
|
|
prompt = part
|
|
resp = llm([HumanMessage(content=prompt), SystemMessage(content=system)])
|
|
summary += resp.content + "\n"
|
|
|
|
hashtagerSystem = "You are a professional note taker. User will provide bullet points from the transcript of a video. You will reply by suggesting simple short hashtags"
|
|
|
|
hashtags = llm([HumanMessage(content=summary), SystemMessage(content=hashtagerSystem)])
|
|
return {
|
|
'statusCode': 200,
|
|
'body': {
|
|
'url': video.url,
|
|
'creator': video.creator,
|
|
'title': video.title,
|
|
'transcript': video.transcript,
|
|
'summary': summary,
|
|
'hashtags': hashtags.content
|
|
}
|
|
}
|
|
|
|
"""
|
|
curl test:
|
|
curl -XPOST --header "Content-Type: application/json" -d "{\"prompt\":\"What is the best way to learn a language?\"}" http://localhost:5000/openai
|
|
|
|
curl -XPOST --header "Content-Type: application/json" -d "{\"prompt\":\"What is a good name for a company that makes funny socks?\"}" http://localhost:5000/openai
|
|
"""
|
|
|
|
|
|
@app.route('/')
|
|
def home():
|
|
return """
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>This is not for human, gtfo</title>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
</head>
|
|
<style>
|
|
body {
|
|
max-width: 100vw;
|
|
max-height: 100vh;
|
|
font-family: monospace;
|
|
text-align: center;
|
|
background-color: #000;
|
|
color: #fff;
|
|
}
|
|
p {
|
|
margin-top: 30vh;
|
|
}
|
|
|
|
</style>
|
|
<body>
|
|
<p>You have no business being here...</p>
|
|
<h1>GTFO</h1>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
@app.route('/translate/<string:lang>', methods=['POST'])
|
|
def translate_text(lang):
|
|
if lang not in models:
|
|
return jsonify(error=f"No model for language: {lang}"), 400
|
|
|
|
content = request.json
|
|
text = content['text']
|
|
translated_text = translate(text, lang)
|
|
return jsonify(translated_text=translated_text)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|
|
|