Update legal.py
Browse files
legal.py
CHANGED
|
@@ -1,266 +1,266 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
-
import speech_recognition as sr
|
| 3 |
-
import threading
|
| 4 |
-
import datetime
|
| 5 |
-
import pyttsx3
|
| 6 |
-
from langdetect import detect
|
| 7 |
-
from huggingface_hub import login
|
| 8 |
-
from sentence_transformers import SentenceTransformer
|
| 9 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering, AutoModelForSeq2SeqLM
|
| 10 |
-
import faiss
|
| 11 |
-
import numpy as np
|
| 12 |
-
import pandas as pd
|
| 13 |
-
import json
|
| 14 |
-
import webbrowser
|
| 15 |
-
from pydub import AudioSegment
|
| 16 |
-
import os
|
| 17 |
-
from werkzeug.utils import secure_filename
|
| 18 |
-
import tempfile
|
| 19 |
-
|
| 20 |
-
app = Flask(__name__, static_folder='.') # Serve static files from the current directory
|
| 21 |
-
|
| 22 |
-
# Load Hugging Face API key from environment variable
|
| 23 |
-
hf_token = os.environ.get("
|
| 24 |
-
if not hf_token:
|
| 25 |
-
# Attempt to load from .env file if not set in environment
|
| 26 |
-
from dotenv import load_dotenv
|
| 27 |
-
load_dotenv()
|
| 28 |
-
hf_token = os.environ.get("API_KEY")
|
| 29 |
-
if not hf_token:
|
| 30 |
-
raise ValueError("Hugging Face API key not found. Please set 'API_KEY' as an environment variable or in a .env file.")
|
| 31 |
-
|
| 32 |
-
login(token=hf_token)
|
| 33 |
-
|
| 34 |
-
# QA Models
|
| 35 |
-
qa_model = AutoModelForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2")
|
| 36 |
-
qa_tokenizer = AutoTokenizer.from_pretrained("deepset/roberta-base-squad2")
|
| 37 |
-
qa_pipeline = pipeline("question-answering", model=qa_model, tokenizer=qa_tokenizer)
|
| 38 |
-
|
| 39 |
-
# Summarization Model
|
| 40 |
-
summarizer_model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
|
| 41 |
-
summarizer_tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
|
| 42 |
-
summarizer_pipeline = pipeline("summarization", model=summarizer_model, tokenizer=summarizer_tokenizer)
|
| 43 |
-
|
| 44 |
-
embed_model = SentenceTransformer("sentence-transformers/paraphrase-MiniLM-L6-v2")
|
| 45 |
-
|
| 46 |
-
# Load both datasets
|
| 47 |
-
df_parquet = pd.read_parquet("ibtehaj dataset.parquet")
|
| 48 |
-
corpus_parquet = df_parquet["text"].dropna().tolist()
|
| 49 |
-
|
| 50 |
-
# Load the JSON dataset
|
| 51 |
-
with open("pdf_data.json", "r", encoding="utf-8") as f:
|
| 52 |
-
json_data = json.load(f)
|
| 53 |
-
|
| 54 |
-
# Extract text from JSON
|
| 55 |
-
corpus_json = []
|
| 56 |
-
for entry in json_data:
|
| 57 |
-
if isinstance(entry, dict) and "text" in entry:
|
| 58 |
-
text = entry["text"].strip()
|
| 59 |
-
if text:
|
| 60 |
-
corpus_json.append(text)
|
| 61 |
-
|
| 62 |
-
# Combine both corpora
|
| 63 |
-
corpus = corpus_parquet + corpus_json
|
| 64 |
-
|
| 65 |
-
# Compute embeddings
|
| 66 |
-
embeddings = embed_model.encode(corpus, show_progress_bar=True, batch_size=16)
|
| 67 |
-
|
| 68 |
-
# Build FAISS index
|
| 69 |
-
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 70 |
-
index.add(np.array(embeddings))
|
| 71 |
-
|
| 72 |
-
def rag_answer(question: str, k: int = 3) -> str:
|
| 73 |
-
q_emb = embed_model.encode([question])
|
| 74 |
-
D, I = index.search(q_emb, k)
|
| 75 |
-
context = "\n\n".join(corpus[i] for i in I[0] if 0 <= i < len(corpus))
|
| 76 |
-
|
| 77 |
-
if not context.strip():
|
| 78 |
-
return "Context is empty. Try rephrasing the question."
|
| 79 |
-
|
| 80 |
-
try:
|
| 81 |
-
result = qa_pipeline(question=question, context=context)
|
| 82 |
-
raw_answer = result.get("answer", "No answer found.")
|
| 83 |
-
|
| 84 |
-
# Summarize if answer is too long (>40 words or 300 characters)
|
| 85 |
-
if len(raw_answer.split()) > 40 or len(raw_answer) > 300:
|
| 86 |
-
summary = summarizer_pipeline(raw_answer, max_length=50, min_length=15, do_sample=False)
|
| 87 |
-
summarized_answer = summary[0]['summary_text']
|
| 88 |
-
else:
|
| 89 |
-
summarized_answer = raw_answer
|
| 90 |
-
|
| 91 |
-
return f"Answer: {summarized_answer}\n\n[Context Used]:\n{context[:500]}..."
|
| 92 |
-
except Exception as e:
|
| 93 |
-
return f"Error: {e}"
|
| 94 |
-
|
| 95 |
-
# Global for TTS engine (to allow stopping)
|
| 96 |
-
tts_engine = None
|
| 97 |
-
|
| 98 |
-
def init_tts_engine():
|
| 99 |
-
global tts_engine
|
| 100 |
-
if tts_engine is None:
|
| 101 |
-
tts_engine = pyttsx3.init()
|
| 102 |
-
tts_engine.setProperty('rate', 150)
|
| 103 |
-
tts_engine.setProperty('volume', 1.0)
|
| 104 |
-
voices = tts_engine.getProperty('voices')
|
| 105 |
-
for v in voices:
|
| 106 |
-
if "zira" in v.name.lower() or "female" in v.name.lower():
|
| 107 |
-
tts_engine.setProperty('voice', v.id)
|
| 108 |
-
break
|
| 109 |
-
|
| 110 |
-
init_tts_engine()
|
| 111 |
-
|
| 112 |
-
# Global variables for managing state (simplify for web context)
|
| 113 |
-
conversation_history = []
|
| 114 |
-
last_question_text = ""
|
| 115 |
-
last_answer_text = ""
|
| 116 |
-
|
| 117 |
-
@app.route('/')
|
| 118 |
-
def serve_index():
|
| 119 |
-
return send_from_directory('.', 'index.html')
|
| 120 |
-
|
| 121 |
-
@app.route('/<path:path>')
|
| 122 |
-
def serve_static_files(path):
|
| 123 |
-
return send_from_directory('.', path)
|
| 124 |
-
|
| 125 |
-
@app.route('/answer', methods=['POST'])
|
| 126 |
-
def generate_answer_endpoint():
|
| 127 |
-
global last_question_text, last_answer_text, conversation_history
|
| 128 |
-
data = request.get_json()
|
| 129 |
-
question = data.get('question', '').strip()
|
| 130 |
-
|
| 131 |
-
if not question:
|
| 132 |
-
return jsonify({"answer": "Please provide a question."}), 400
|
| 133 |
-
|
| 134 |
-
last_question_text = question
|
| 135 |
-
timestamp = datetime.datetime.now().strftime("%H:%M:%S")
|
| 136 |
-
conversation_history.append({"role": "user", "time": timestamp, "text": question})
|
| 137 |
-
|
| 138 |
-
ans = rag_answer(question)
|
| 139 |
-
last_answer_text = ans
|
| 140 |
-
conversation_history.append({"role": "bot", "time": timestamp, "text": ans})
|
| 141 |
-
|
| 142 |
-
return jsonify({"answer": ans})
|
| 143 |
-
|
| 144 |
-
@app.route('/read-aloud', methods=['POST'])
|
| 145 |
-
def read_aloud_endpoint():
|
| 146 |
-
data = request.get_json()
|
| 147 |
-
text_to_read = data.get('text', '').strip()
|
| 148 |
-
|
| 149 |
-
if not text_to_read:
|
| 150 |
-
return jsonify({"status": "No text provided to read."}), 400
|
| 151 |
-
|
| 152 |
-
try:
|
| 153 |
-
# Create a temporary file for the speech audio
|
| 154 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 155 |
-
temp_audio_path = fp.name
|
| 156 |
-
|
| 157 |
-
tts_engine.save_to_file(text_to_read, temp_audio_path)
|
| 158 |
-
tts_engine.runAndWait()
|
| 159 |
-
|
| 160 |
-
# You would typically serve this file or stream it.
|
| 161 |
-
# For simplicity, let's just confirm it was generated.
|
| 162 |
-
# In a real app, you might use Flask's send_file for audio playback.
|
| 163 |
-
# For now, let's just return success.
|
| 164 |
-
# This approach is suitable if the browser requests the audio file directly after this.
|
| 165 |
-
# For direct playback, you might stream it or serve it immediately.
|
| 166 |
-
# For web, it's more common to have the frontend's SpeechSynthesis API handle this.
|
| 167 |
-
# The frontend `readAloud` function already does this.
|
| 168 |
-
# So, this endpoint might not be strictly necessary unless for server-side TTS.
|
| 169 |
-
return jsonify({"status": "TTS audio generated (server-side)."})
|
| 170 |
-
except Exception as e:
|
| 171 |
-
return jsonify({"status": f"Error during TTS: {str(e)}"}), 500
|
| 172 |
-
finally:
|
| 173 |
-
if os.path.exists(temp_audio_path):
|
| 174 |
-
os.remove(temp_audio_path)
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
@app.route('/upload-mp3', methods=['POST'])
|
| 178 |
-
def upload_mp3_endpoint():
|
| 179 |
-
global last_question_text, last_answer_text, conversation_history
|
| 180 |
-
|
| 181 |
-
if 'file' not in request.files:
|
| 182 |
-
return jsonify({"message": "No file part"}), 400
|
| 183 |
-
file = request.files['file']
|
| 184 |
-
if file.filename == '':
|
| 185 |
-
return jsonify({"message": "No selected file"}), 400
|
| 186 |
-
if file:
|
| 187 |
-
filename = secure_filename(file.filename)
|
| 188 |
-
# Create a temporary directory to save the uploaded file and its WAV conversion
|
| 189 |
-
with tempfile.TemporaryDirectory() as tmpdir:
|
| 190 |
-
mp3_path = os.path.join(tmpdir, filename)
|
| 191 |
-
file.save(mp3_path)
|
| 192 |
-
|
| 193 |
-
wav_path = os.path.join(tmpdir, filename.replace(".mp3", ".wav"))
|
| 194 |
-
try:
|
| 195 |
-
sound = AudioSegment.from_mp3(mp3_path)
|
| 196 |
-
sound.export(wav_path, format="wav")
|
| 197 |
-
except Exception as e:
|
| 198 |
-
return jsonify({"message": f"Error converting MP3 to WAV: {e}"}), 500
|
| 199 |
-
|
| 200 |
-
try:
|
| 201 |
-
recognizer = sr.Recognizer()
|
| 202 |
-
with sr.AudioFile(wav_path) as src:
|
| 203 |
-
audio = recognizer.record(src)
|
| 204 |
-
text = recognizer.recognize_google(audio)
|
| 205 |
-
except sr.UnknownValueError:
|
| 206 |
-
return jsonify({"message": "Speech not understood."}), 400
|
| 207 |
-
except sr.RequestError as e:
|
| 208 |
-
return jsonify({"message": f"Speech recognition service error: {e}"}), 500
|
| 209 |
-
|
| 210 |
-
# Store transcription temporarily (can be handled differently)
|
| 211 |
-
transcript_path = os.path.join(tmpdir, "transcription.txt")
|
| 212 |
-
with open(transcript_path, "w", encoding="utf-8") as f:
|
| 213 |
-
f.write(text)
|
| 214 |
-
|
| 215 |
-
# Option to summarize or generate answer from transcription
|
| 216 |
-
# For this web integration, we'll return the transcription and let frontend decide
|
| 217 |
-
return jsonify({
|
| 218 |
-
"message": "MP3 transcribed successfully.",
|
| 219 |
-
"transcription": text
|
| 220 |
-
})
|
| 221 |
-
|
| 222 |
-
@app.route('/summarize', methods=['POST'])
|
| 223 |
-
def summarize_endpoint():
|
| 224 |
-
data = request.get_json()
|
| 225 |
-
text_to_summarize = data.get('text', '').strip()
|
| 226 |
-
|
| 227 |
-
if not text_to_summarize:
|
| 228 |
-
return jsonify({"summary": "No text provided for summarization."}), 400
|
| 229 |
-
|
| 230 |
-
def chunk_text(text, max_chunk_size=4000):
|
| 231 |
-
sentences = text.split(". ")
|
| 232 |
-
chunks = []
|
| 233 |
-
current_chunk = ""
|
| 234 |
-
for sentence in sentences:
|
| 235 |
-
# Add sentence length + 2 for ". "
|
| 236 |
-
if len(current_chunk) + len(sentence) + 2 < max_chunk_size:
|
| 237 |
-
current_chunk += sentence + ". "
|
| 238 |
-
else:
|
| 239 |
-
chunks.append(current_chunk.strip())
|
| 240 |
-
current_chunk = sentence + ". "
|
| 241 |
-
if current_chunk:
|
| 242 |
-
chunks.append(current_chunk.strip())
|
| 243 |
-
return chunks
|
| 244 |
-
|
| 245 |
-
try:
|
| 246 |
-
chunks = chunk_text(text_to_summarize)
|
| 247 |
-
summaries = [
|
| 248 |
-
summarizer_pipeline(chunk, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
| 249 |
-
for chunk in chunks
|
| 250 |
-
]
|
| 251 |
-
final_input = " ".join(summaries)
|
| 252 |
-
final_summary = summarizer_pipeline(final_input, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
| 253 |
-
return jsonify({"summary": final_summary})
|
| 254 |
-
except Exception as e:
|
| 255 |
-
return jsonify({"summary": f"Error during summarization: {e}"}), 500
|
| 256 |
-
|
| 257 |
-
@app.route('/history', methods=['GET'])
|
| 258 |
-
def get_history():
|
| 259 |
-
return jsonify({"history": conversation_history})
|
| 260 |
-
|
| 261 |
-
if __name__ == '__main__':
|
| 262 |
-
# Make sure your datasets are in the same directory as app.py
|
| 263 |
-
# ibtehaj dataset.parquet
|
| 264 |
-
# pdf_data.json
|
| 265 |
-
# man.jpg (for the image)
|
| 266 |
app.run(debug=True) # debug=True allows for automatic reloading on code changes
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
import threading
|
| 4 |
+
import datetime
|
| 5 |
+
import pyttsx3
|
| 6 |
+
from langdetect import detect
|
| 7 |
+
from huggingface_hub import login
|
| 8 |
+
from sentence_transformers import SentenceTransformer
|
| 9 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering, AutoModelForSeq2SeqLM
|
| 10 |
+
import faiss
|
| 11 |
+
import numpy as np
|
| 12 |
+
import pandas as pd
|
| 13 |
+
import json
|
| 14 |
+
import webbrowser
|
| 15 |
+
from pydub import AudioSegment
|
| 16 |
+
import os
|
| 17 |
+
from werkzeug.utils import secure_filename
|
| 18 |
+
import tempfile
|
| 19 |
+
|
| 20 |
+
app = Flask(__name__, static_folder='.') # Serve static files from the current directory
|
| 21 |
+
|
| 22 |
+
# Load Hugging Face API key from environment variable
|
| 23 |
+
hf_token = os.environ.get("api")
|
| 24 |
+
if not hf_token:
|
| 25 |
+
# Attempt to load from .env file if not set in environment
|
| 26 |
+
from dotenv import load_dotenv
|
| 27 |
+
load_dotenv()
|
| 28 |
+
hf_token = os.environ.get("API_KEY")
|
| 29 |
+
if not hf_token:
|
| 30 |
+
raise ValueError("Hugging Face API key not found. Please set 'API_KEY' as an environment variable or in a .env file.")
|
| 31 |
+
|
| 32 |
+
login(token=hf_token)
|
| 33 |
+
|
| 34 |
+
# QA Models
|
| 35 |
+
qa_model = AutoModelForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2")
|
| 36 |
+
qa_tokenizer = AutoTokenizer.from_pretrained("deepset/roberta-base-squad2")
|
| 37 |
+
qa_pipeline = pipeline("question-answering", model=qa_model, tokenizer=qa_tokenizer)
|
| 38 |
+
|
| 39 |
+
# Summarization Model
|
| 40 |
+
summarizer_model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
|
| 41 |
+
summarizer_tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
|
| 42 |
+
summarizer_pipeline = pipeline("summarization", model=summarizer_model, tokenizer=summarizer_tokenizer)
|
| 43 |
+
|
| 44 |
+
embed_model = SentenceTransformer("sentence-transformers/paraphrase-MiniLM-L6-v2")
|
| 45 |
+
|
| 46 |
+
# Load both datasets
|
| 47 |
+
df_parquet = pd.read_parquet("ibtehaj dataset.parquet")
|
| 48 |
+
corpus_parquet = df_parquet["text"].dropna().tolist()
|
| 49 |
+
|
| 50 |
+
# Load the JSON dataset
|
| 51 |
+
with open("pdf_data.json", "r", encoding="utf-8") as f:
|
| 52 |
+
json_data = json.load(f)
|
| 53 |
+
|
| 54 |
+
# Extract text from JSON
|
| 55 |
+
corpus_json = []
|
| 56 |
+
for entry in json_data:
|
| 57 |
+
if isinstance(entry, dict) and "text" in entry:
|
| 58 |
+
text = entry["text"].strip()
|
| 59 |
+
if text:
|
| 60 |
+
corpus_json.append(text)
|
| 61 |
+
|
| 62 |
+
# Combine both corpora
|
| 63 |
+
corpus = corpus_parquet + corpus_json
|
| 64 |
+
|
| 65 |
+
# Compute embeddings
|
| 66 |
+
embeddings = embed_model.encode(corpus, show_progress_bar=True, batch_size=16)
|
| 67 |
+
|
| 68 |
+
# Build FAISS index
|
| 69 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 70 |
+
index.add(np.array(embeddings))
|
| 71 |
+
|
| 72 |
+
def rag_answer(question: str, k: int = 3) -> str:
|
| 73 |
+
q_emb = embed_model.encode([question])
|
| 74 |
+
D, I = index.search(q_emb, k)
|
| 75 |
+
context = "\n\n".join(corpus[i] for i in I[0] if 0 <= i < len(corpus))
|
| 76 |
+
|
| 77 |
+
if not context.strip():
|
| 78 |
+
return "Context is empty. Try rephrasing the question."
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
result = qa_pipeline(question=question, context=context)
|
| 82 |
+
raw_answer = result.get("answer", "No answer found.")
|
| 83 |
+
|
| 84 |
+
# Summarize if answer is too long (>40 words or 300 characters)
|
| 85 |
+
if len(raw_answer.split()) > 40 or len(raw_answer) > 300:
|
| 86 |
+
summary = summarizer_pipeline(raw_answer, max_length=50, min_length=15, do_sample=False)
|
| 87 |
+
summarized_answer = summary[0]['summary_text']
|
| 88 |
+
else:
|
| 89 |
+
summarized_answer = raw_answer
|
| 90 |
+
|
| 91 |
+
return f"Answer: {summarized_answer}\n\n[Context Used]:\n{context[:500]}..."
|
| 92 |
+
except Exception as e:
|
| 93 |
+
return f"Error: {e}"
|
| 94 |
+
|
| 95 |
+
# Global for TTS engine (to allow stopping)
|
| 96 |
+
tts_engine = None
|
| 97 |
+
|
| 98 |
+
def init_tts_engine():
|
| 99 |
+
global tts_engine
|
| 100 |
+
if tts_engine is None:
|
| 101 |
+
tts_engine = pyttsx3.init()
|
| 102 |
+
tts_engine.setProperty('rate', 150)
|
| 103 |
+
tts_engine.setProperty('volume', 1.0)
|
| 104 |
+
voices = tts_engine.getProperty('voices')
|
| 105 |
+
for v in voices:
|
| 106 |
+
if "zira" in v.name.lower() or "female" in v.name.lower():
|
| 107 |
+
tts_engine.setProperty('voice', v.id)
|
| 108 |
+
break
|
| 109 |
+
|
| 110 |
+
init_tts_engine()
|
| 111 |
+
|
| 112 |
+
# Global variables for managing state (simplify for web context)
|
| 113 |
+
conversation_history = []
|
| 114 |
+
last_question_text = ""
|
| 115 |
+
last_answer_text = ""
|
| 116 |
+
|
| 117 |
+
@app.route('/')
|
| 118 |
+
def serve_index():
|
| 119 |
+
return send_from_directory('.', 'index.html')
|
| 120 |
+
|
| 121 |
+
@app.route('/<path:path>')
|
| 122 |
+
def serve_static_files(path):
|
| 123 |
+
return send_from_directory('.', path)
|
| 124 |
+
|
| 125 |
+
@app.route('/answer', methods=['POST'])
|
| 126 |
+
def generate_answer_endpoint():
|
| 127 |
+
global last_question_text, last_answer_text, conversation_history
|
| 128 |
+
data = request.get_json()
|
| 129 |
+
question = data.get('question', '').strip()
|
| 130 |
+
|
| 131 |
+
if not question:
|
| 132 |
+
return jsonify({"answer": "Please provide a question."}), 400
|
| 133 |
+
|
| 134 |
+
last_question_text = question
|
| 135 |
+
timestamp = datetime.datetime.now().strftime("%H:%M:%S")
|
| 136 |
+
conversation_history.append({"role": "user", "time": timestamp, "text": question})
|
| 137 |
+
|
| 138 |
+
ans = rag_answer(question)
|
| 139 |
+
last_answer_text = ans
|
| 140 |
+
conversation_history.append({"role": "bot", "time": timestamp, "text": ans})
|
| 141 |
+
|
| 142 |
+
return jsonify({"answer": ans})
|
| 143 |
+
|
| 144 |
+
@app.route('/read-aloud', methods=['POST'])
|
| 145 |
+
def read_aloud_endpoint():
|
| 146 |
+
data = request.get_json()
|
| 147 |
+
text_to_read = data.get('text', '').strip()
|
| 148 |
+
|
| 149 |
+
if not text_to_read:
|
| 150 |
+
return jsonify({"status": "No text provided to read."}), 400
|
| 151 |
+
|
| 152 |
+
try:
|
| 153 |
+
# Create a temporary file for the speech audio
|
| 154 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 155 |
+
temp_audio_path = fp.name
|
| 156 |
+
|
| 157 |
+
tts_engine.save_to_file(text_to_read, temp_audio_path)
|
| 158 |
+
tts_engine.runAndWait()
|
| 159 |
+
|
| 160 |
+
# You would typically serve this file or stream it.
|
| 161 |
+
# For simplicity, let's just confirm it was generated.
|
| 162 |
+
# In a real app, you might use Flask's send_file for audio playback.
|
| 163 |
+
# For now, let's just return success.
|
| 164 |
+
# This approach is suitable if the browser requests the audio file directly after this.
|
| 165 |
+
# For direct playback, you might stream it or serve it immediately.
|
| 166 |
+
# For web, it's more common to have the frontend's SpeechSynthesis API handle this.
|
| 167 |
+
# The frontend `readAloud` function already does this.
|
| 168 |
+
# So, this endpoint might not be strictly necessary unless for server-side TTS.
|
| 169 |
+
return jsonify({"status": "TTS audio generated (server-side)."})
|
| 170 |
+
except Exception as e:
|
| 171 |
+
return jsonify({"status": f"Error during TTS: {str(e)}"}), 500
|
| 172 |
+
finally:
|
| 173 |
+
if os.path.exists(temp_audio_path):
|
| 174 |
+
os.remove(temp_audio_path)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
@app.route('/upload-mp3', methods=['POST'])
|
| 178 |
+
def upload_mp3_endpoint():
|
| 179 |
+
global last_question_text, last_answer_text, conversation_history
|
| 180 |
+
|
| 181 |
+
if 'file' not in request.files:
|
| 182 |
+
return jsonify({"message": "No file part"}), 400
|
| 183 |
+
file = request.files['file']
|
| 184 |
+
if file.filename == '':
|
| 185 |
+
return jsonify({"message": "No selected file"}), 400
|
| 186 |
+
if file:
|
| 187 |
+
filename = secure_filename(file.filename)
|
| 188 |
+
# Create a temporary directory to save the uploaded file and its WAV conversion
|
| 189 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 190 |
+
mp3_path = os.path.join(tmpdir, filename)
|
| 191 |
+
file.save(mp3_path)
|
| 192 |
+
|
| 193 |
+
wav_path = os.path.join(tmpdir, filename.replace(".mp3", ".wav"))
|
| 194 |
+
try:
|
| 195 |
+
sound = AudioSegment.from_mp3(mp3_path)
|
| 196 |
+
sound.export(wav_path, format="wav")
|
| 197 |
+
except Exception as e:
|
| 198 |
+
return jsonify({"message": f"Error converting MP3 to WAV: {e}"}), 500
|
| 199 |
+
|
| 200 |
+
try:
|
| 201 |
+
recognizer = sr.Recognizer()
|
| 202 |
+
with sr.AudioFile(wav_path) as src:
|
| 203 |
+
audio = recognizer.record(src)
|
| 204 |
+
text = recognizer.recognize_google(audio)
|
| 205 |
+
except sr.UnknownValueError:
|
| 206 |
+
return jsonify({"message": "Speech not understood."}), 400
|
| 207 |
+
except sr.RequestError as e:
|
| 208 |
+
return jsonify({"message": f"Speech recognition service error: {e}"}), 500
|
| 209 |
+
|
| 210 |
+
# Store transcription temporarily (can be handled differently)
|
| 211 |
+
transcript_path = os.path.join(tmpdir, "transcription.txt")
|
| 212 |
+
with open(transcript_path, "w", encoding="utf-8") as f:
|
| 213 |
+
f.write(text)
|
| 214 |
+
|
| 215 |
+
# Option to summarize or generate answer from transcription
|
| 216 |
+
# For this web integration, we'll return the transcription and let frontend decide
|
| 217 |
+
return jsonify({
|
| 218 |
+
"message": "MP3 transcribed successfully.",
|
| 219 |
+
"transcription": text
|
| 220 |
+
})
|
| 221 |
+
|
| 222 |
+
@app.route('/summarize', methods=['POST'])
|
| 223 |
+
def summarize_endpoint():
|
| 224 |
+
data = request.get_json()
|
| 225 |
+
text_to_summarize = data.get('text', '').strip()
|
| 226 |
+
|
| 227 |
+
if not text_to_summarize:
|
| 228 |
+
return jsonify({"summary": "No text provided for summarization."}), 400
|
| 229 |
+
|
| 230 |
+
def chunk_text(text, max_chunk_size=4000):
|
| 231 |
+
sentences = text.split(". ")
|
| 232 |
+
chunks = []
|
| 233 |
+
current_chunk = ""
|
| 234 |
+
for sentence in sentences:
|
| 235 |
+
# Add sentence length + 2 for ". "
|
| 236 |
+
if len(current_chunk) + len(sentence) + 2 < max_chunk_size:
|
| 237 |
+
current_chunk += sentence + ". "
|
| 238 |
+
else:
|
| 239 |
+
chunks.append(current_chunk.strip())
|
| 240 |
+
current_chunk = sentence + ". "
|
| 241 |
+
if current_chunk:
|
| 242 |
+
chunks.append(current_chunk.strip())
|
| 243 |
+
return chunks
|
| 244 |
+
|
| 245 |
+
try:
|
| 246 |
+
chunks = chunk_text(text_to_summarize)
|
| 247 |
+
summaries = [
|
| 248 |
+
summarizer_pipeline(chunk, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
| 249 |
+
for chunk in chunks
|
| 250 |
+
]
|
| 251 |
+
final_input = " ".join(summaries)
|
| 252 |
+
final_summary = summarizer_pipeline(final_input, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
| 253 |
+
return jsonify({"summary": final_summary})
|
| 254 |
+
except Exception as e:
|
| 255 |
+
return jsonify({"summary": f"Error during summarization: {e}"}), 500
|
| 256 |
+
|
| 257 |
+
@app.route('/history', methods=['GET'])
|
| 258 |
+
def get_history():
|
| 259 |
+
return jsonify({"history": conversation_history})
|
| 260 |
+
|
| 261 |
+
if __name__ == '__main__':
|
| 262 |
+
# Make sure your datasets are in the same directory as app.py
|
| 263 |
+
# ibtehaj dataset.parquet
|
| 264 |
+
# pdf_data.json
|
| 265 |
+
# man.jpg (for the image)
|
| 266 |
app.run(debug=True) # debug=True allows for automatic reloading on code changes
|