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 """
You have no business being here...