Spaces:
Sleeping
Sleeping
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 | |
def home(): | |
return "Flask API is running!" | |
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") | |