yolloo commited on
Commit
28f7ad4
·
verified ·
1 Parent(s): cd5e05d

Upload 3 files

Browse files
Files changed (3) hide show
  1. qamatcher_server.py +69 -0
  2. qgen_server.py +38 -0
  3. whisper_server (1).py +61 -0
qamatcher_server.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ from flask import request, jsonify
4
+ from sentence_transformers import SentenceTransformer, util
5
+
6
+ # Define a writable directory for the model cache
7
+ cache_dir = os.path.join(os.getenv("XDG_CACHE_HOME", "/tmp/.cache"), "huggingface_models")
8
+ os.makedirs(cache_dir, exist_ok=True)
9
+
10
+ print("Loading SentenceTransformer model (paraphrase-MiniLM-L6-v2)...")
11
+ matcher_model = SentenceTransformer('paraphrase-MiniLM-L6-v2', cache_folder=cache_dir)
12
+ print("SentenceTransformer model loaded.")
13
+
14
+ # Define a threshold for a "good" match
15
+ SIMILARITY_THRESHOLD = 0.6
16
+
17
+ def handle_match_question():
18
+ data = request.get_json()
19
+ if not data or 'user_question' not in data or 'documents' not in data:
20
+ return jsonify({'error': 'Invalid request. "user_question" and "documents" are required.'}), 400
21
+
22
+ user_question = data['user_question']
23
+ documents = data['documents']
24
+
25
+ if not documents:
26
+ return jsonify({'answer': "There are no notes to search."})
27
+
28
+ # Flatten the list of questions from all documents
29
+ all_questions = []
30
+ # Map each question to the original note text
31
+ question_to_note_map = {}
32
+
33
+ for doc in documents:
34
+ note_text = doc.get('note_text', '')
35
+ for q in doc.get('questions', []):
36
+ all_questions.append(q)
37
+ question_to_note_map[q] = note_text
38
+
39
+ if not all_questions:
40
+ return jsonify({'answer': "No questions have been generated for your notes yet."})
41
+
42
+ try:
43
+ # Encode the user's question and all stored questions
44
+ user_embedding = matcher_model.encode(user_question, convert_to_tensor=True)
45
+ stored_embeddings = matcher_model.encode(all_questions, convert_to_tensor=True)
46
+
47
+ # Compute cosine similarity
48
+ cosine_scores = util.pytorch_cos_sim(user_embedding, stored_embeddings)
49
+
50
+ # Find the best match
51
+ best_match_idx = cosine_scores.argmax()
52
+ best_score = float(cosine_scores[0][best_match_idx])
53
+ best_question = all_questions[best_match_idx]
54
+
55
+ print(f"User Question: '{user_question}'")
56
+ print(f"Best matched stored question: '{best_question}' with score: {best_score:.4f}")
57
+
58
+ # Check if the match is good enough
59
+ if best_score > SIMILARITY_THRESHOLD:
60
+ # Return the note associated with the best-matched question
61
+ answer = question_to_note_map[best_question]
62
+ else:
63
+ answer = "Sorry, I couldn't find a relevant note to answer your question."
64
+
65
+ return jsonify({'answer': answer})
66
+
67
+ except Exception as e:
68
+ print(f"Error during question matching: {e}")
69
+ return jsonify({'error': str(e)}), 500
qgen_server.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ from flask import request, jsonify
4
+ from transformers import pipeline
5
+
6
+ # Define a writable directory for the model cache
7
+ cache_dir = os.path.join(os.getenv("XDG_CACHE_HOME", "/tmp/.cache"), "huggingface_models")
8
+ os.makedirs(cache_dir, exist_ok=True)
9
+
10
+ print("Loading Question Generation model (iarfmoose/t5-base-question-generator)...")
11
+ # Initialize the pipeline for text2text-generation with the specified model
12
+ qg_model = pipeline("text2text-generation", model="iarfmoose/t5-base-question-generator", model_kwargs={"cache_dir": cache_dir})
13
+ print("Question Generation model loaded.")
14
+
15
+ def handle_generate_questions():
16
+ data = request.get_json()
17
+ if not data or 'text' not in data:
18
+ return jsonify({'error': 'Invalid request. "text" field is required.'}), 400
19
+
20
+ text = data['text']
21
+
22
+ # Prepend the text with "generate questions: " as required by this model
23
+ input_text = f"generate questions: {text}"
24
+
25
+ try:
26
+ # Generate questions
27
+ results = qg_model(input_text, max_length=64, num_beams=4, early_stopping=True)
28
+
29
+ # The result is a single string with questions separated by '<sep>'
30
+ generated_text = results[0]['generated_text']
31
+ questions = [q.strip() for q in generated_text.split('<sep>') if q.strip()]
32
+
33
+ print(f"Generated questions for text: '{text[:50]}...' -> {questions}")
34
+
35
+ return jsonify({'questions': questions})
36
+ except Exception as e:
37
+ print(f"Error during question generation: {e}")
38
+ return jsonify({'error': str(e)}), 500
whisper_server (1).py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import tempfile
4
+ from flask import request, jsonify
5
+ from transformers import pipeline
6
+ import torch
7
+
8
+ # Define a writable directory for the model cache
9
+ cache_dir = os.path.join(os.getenv("XDG_CACHE_HOME", "/tmp/.cache"), "huggingface_models")
10
+ os.makedirs(cache_dir, exist_ok=True)
11
+
12
+ print("Loading collabora/whisper-tiny-hindi model via transformers pipeline...")
13
+
14
+ # Determine device
15
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
16
+
17
+ # Initialize the ASR pipeline with the specified model
18
+ # Using the transformers pipeline is the correct way to load custom models from the Hub.
19
+ model = pipeline(
20
+ "automatic-speech-recognition",
21
+ model="collabora/whisper-tiny-hindi",
22
+ device=device,
23
+ model_kwargs={"cache_dir": cache_dir}
24
+ )
25
+
26
+ print("Whisper model loaded.")
27
+
28
+ def handle_transcribe():
29
+ if 'file' not in request.files:
30
+ return jsonify({'error': 'No file part in the request'}), 400
31
+
32
+ file = request.files['file']
33
+
34
+ if file.filename == '':
35
+ return jsonify({'error': 'No selected file'}), 400
36
+
37
+ if file:
38
+ # Use a temporary file to save the upload
39
+ with tempfile.NamedTemporaryFile(delete=True, suffix=".webm") as temp_audio:
40
+ file.save(temp_audio.name)
41
+
42
+ try:
43
+ print(f"Transcribing file: {temp_audio.name} with collabora/whisper-tiny-hindi pipeline")
44
+
45
+ # The pipeline expects a file path and handles the processing.
46
+ result = model(temp_audio.name)
47
+
48
+ transcribed_text = result.get('text', '')
49
+
50
+ print("Transcription successful.")
51
+ return jsonify({'text': transcribed_text})
52
+ except Exception as e:
53
+ print(f"Error during transcription: {e}")
54
+ # Provide a more specific error if possible
55
+ error_message = f"An unexpected error occurred during transcription: {str(e)}"
56
+ if "out of memory" in str(e).lower():
57
+ error_message = "The model ran out of memory. Please try a smaller audio file or check server resources."
58
+
59
+ return jsonify({'error': error_message}), 500
60
+
61
+ return jsonify({'error': 'File processing failed'}), 500