Spaces:
Sleeping
Sleeping
File size: 1,817 Bytes
08c11be f9bbe87 08c11be 94103b4 08c11be d0796dd 08c11be d0796dd 08c11be f9bbe87 d92d5ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
from flask import Flask, request, jsonify
from aiohttp import web, WSMsgType
from flask_cors import CORS
import requests
import os
import PyPDF2 # <-- new import
app = Flask(__name__)
CORS(app)
OPENAI_API_URL = "https://api.openai.com/v1/realtime"
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise ValueError("OPENAI_API_KEY environment variable not set!")
# Function to extract text from your PDF
def extract_pdf_text(pdf_path):
pdf_text = ""
with open(pdf_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
for page in reader.pages:
pdf_text += page.extract_text()
return pdf_text
# Preload your PDF content (or load dynamically if preferred)
PDF_PATH = "resume.pdf" # <-- Path to your PDF
pdf_content = extract_pdf_text(PDF_PATH)
full_text="you are Soumyajit's AI assisstant, you greet by saying- how can I help you about Soumyajit, answer by wrapping the answers about soumyajit, and keep it as short as possible, "
full_text+=pdf_content
@app.route('/')
def home():
return "Flask API is running!"
@app.route('/api/rtc-connect', methods=['POST'])
def connect_rtc():
body = request.get_data(as_text=True)
# Build the OpenAI API request URL
content=f''
url = f"{OPENAI_API_URL}?model=gpt-4o-realtime-preview-2024-12-17&instructions={full_text}&voice=ash"
headers = {
"Authorization": f"Bearer {OPENAI_API_KEY}",
"Content-Type": "application/sdp"
}
# Send POST request to the OpenAI API
response = requests.post(url, headers=headers, data=body)
return response.content, 200, {'Content-Type': 'application/sdp'}
if __name__ == '__main__':
port = int(os.environ.get("PORT", 7860))
app.run(port=port, host="0.0.0.0")
|