File size: 9,456 Bytes
cb4264a
 
 
 
 
 
 
40cc1ec
cb4264a
 
1c2e490
cb4264a
 
 
 
 
 
 
 
31bb5f0
cb4264a
 
 
 
 
 
 
 
 
 
 
 
 
e484140
cb4264a
 
e484140
47c42c3
cb4264a
 
 
 
7a765bf
cb4264a
 
 
 
 
 
 
 
 
 
 
 
e484140
47c42c3
cb4264a
 
 
7a765bf
cb4264a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e484140
cb4264a
 
 
 
47c42c3
cb4264a
 
dd62a18
cb4264a
 
82e7fda
 
cb4264a
 
 
 
 
82e7fda
cb4264a
 
 
1b3c1fb
e484140
47c42c3
cb4264a
1b3c1fb
 
cb4264a
7a765bf
1b3c1fb
cb4264a
 
1b3c1fb
 
 
 
cb4264a
 
 
 
f087dba
 
ddf1747
 
69a7809
ddf1747
 
69a7809
ddf1747
 
69a7809
ddf1747
 
69a7809
ddf1747
 
69a7809
ddf1747
 
69a7809
ddf1747
 
69a7809
ddf1747
1c2e490
 
 
cb4264a
bbb2f78
cb4264a
 
 
 
1b3c1fb
 
 
31bb5f0
 
cb4264a
 
 
 
 
 
 
 
141482d
c1ba1af
cb4264a
c1ba1af
 
cb4264a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1ba1af
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import time
import json
import asyncio
import numpy as np
import soundfile as sf
from datetime import datetime
# from pydub import AudioSegment

# External dependencies.
from flask import Flask, request, jsonify, send_file,render_template

# Import your modules.
from app.stt import AudioProcessor
from app.llm import LLMProcessor
from app.vectorstore import vectorstore
from app.tts import tts
from app.utils import remove_stars


# Set a custom user agent.
os.environ["USER_AGENT"] = "my-app/1.0"

# Rename your class to avoid confusion with Flask's app instance.
class CancerApp:
    def __init__(self, vectorstore_index_path):
        self.audio_processor = AudioProcessor()
        self.llm_processor = LLMProcessor()
        self.vectorstore = vectorstore(vectorstore_index_path, Initlize_with=3)
        self.vectorstore_index = self.vectorstore.load_vectorstore(vectorstore_index_path)
        # Run an initial search to load vectorstore contents.
        self.vectorstore.search_vectorstore("cancer", 1)
        self.tts = tts()
        self.Number_of_search_result_from_vectordb = 10

    def text_to_speech(self, message, output_filename, Saved_response=""):
        search_results = self.vectorstore.search_vectorstore(message, self.Number_of_search_result_from_vectordb)
        query = Saved_response
        for i, result in enumerate(search_results, 1):
            query += f"\n{i}. {result}"
        
        try:
            gemini_response = self.llm_processor.call_gemini_llm("gemini-2.0-flash", message, query)
        except Exception as e:
            print("Gemini error:", e)
            gemini_response = f"Error: {str(e)}"
        gemini_response = remove_stars(gemini_response)
        Saved_response += "message: " + message + "\n" + "gemini_response: " + gemini_response + "\n"
        self.audio_processor.log_conversation(message, bot_text=gemini_response)
        audio_data, sample_rate = asyncio.run(self.tts.cpu_stream_to_audio(gemini_response))
        sf.write(output_filename, audio_data, sample_rate)
        return Saved_response

    def speech_to_speech(self, audiofile_path, output_filename, Saved_response=""):
        transcription = self.audio_processor.transcribe_audio(audiofile_path, language="en")
        search_results = self.vectorstore.search_vectorstore(transcription, self.Number_of_search_result_from_vectordb)
        query = Saved_response  
        for i, result in enumerate(search_results, 1):
            query += f"\n{i}. {result}"
        try:
            gemini_response = self.llm_processor.call_gemini_llm("gemini-2.0-flash",  transcription, query)
        except Exception as e:
            print("Gemini error:", e)
            gemini_response = f"Error: {str(e)}"
        gemini_response = remove_stars(gemini_response)
        Saved_response += "transcription: " + transcription + "\n" + "gemini_response: " + gemini_response + "\n"
        self.audio_processor.log_conversation(transcription, bot_text=gemini_response)
        audio_data, sample_rate = asyncio.run(self.tts.cpu_stream_to_audio(gemini_response))
        sf.write(output_filename, audio_data, sample_rate)
        return Saved_response

    def speech_to_text(self, audiofile_path, Saved_response=""):
        try:
            transcription = self.audio_processor.transcribe_audio(audiofile_path, language="en")
        except Exception as ex:
            print(f"Transcription error: {ex}")
            transcription = ""
        
        if not transcription:
            print("No transcription available; aborting further processing.")
            return "", Saved_response

        try:
            search_results = self.vectorstore.search_vectorstore(transcription, self.Number_of_search_result_from_vectordb)
        except Exception as ex:
            print(f"Vectorstore search error: {ex}")
            search_results = []
        
        query = f"{Saved_response}"
        for i, result in enumerate(search_results, 1):
            query += f"\n{i}. {result}"

        
        try:
            message = f"Old_conversation:{Saved_response} , message:{transcription}" 
            gemini_response = self.llm_processor.call_gemini_llm("gemini-2.0-flash", message,query)
            gemini_response = remove_stars(gemini_response)
        except Exception as e:
            print("Gemini error:", e)
            gemini_response = f"Error: {str(e)}"
        gemini_response = remove_stars(gemini_response)
        Saved_response = f"transcription: {transcription}\n" + f"gemini_response: {gemini_response}\n" + f"Old_conversation:{Saved_response}"
        self.audio_processor.log_conversation(transcription, bot_text=gemini_response)
        return gemini_response, Saved_response

    def text_to_text(self, message, Saved_response=""):
        search_results = self.vectorstore.search_vectorstore(message, self.Number_of_search_result_from_vectordb)
        query = Saved_response
        for i, result in enumerate(search_results, 1):
            query += f"\n{i}. {result}"
        
        try:
            gemini_response = self.llm_processor.call_gemini_llm("gemini-2.0-flash",message,query)
            gemini_response = remove_stars(gemini_response)
        except Exception as e:
            print("Gemini error:", e)
            gemini_response = f"Error: {str(e)}"
        Saved_response += "message: " + message + "\n" + "gemini_response: " + gemini_response + "\n"
        self.audio_processor.log_conversation(message, bot_text=gemini_response)
        return gemini_response, Saved_response

# Create Flask API instance.
flask_app = Flask(__name__)

vectordb = vectorstore(path="data/PDF/Cancer The Evolutionary Legacy .pdf", Initlize_with=1)
print("done creating vectorstore")
vectordb.add_to_vectorstore_from_pdf("data/PDF/cancer_dictionary.pdf")
print("done adding cancer dictionary")

vectordb.add_to_vectorstore_from_pdf("data/PDF/Colon and Other GastrointestinalCancers.pdf")
print("done adding colon and other gastrointestinal cancers")

vectordb.add_to_vectorstore_from_pdf("data/PDF/Medical Dictionary.pdf")
print("done adding medical dictionary")

vectordb.add_to_vectorstore_from_pdf("data/PDF/Molecular biology of cancer.pdf")
print("done adding molecular biology of cancer")

vectordb.add_to_vectorstore_from_pdf("data/PDF/The biology of cancer.pdf")
print("done adding the biology of cancer")

vectordb.add_to_vectorstore_from_pdf("data/PDF/Being mortal _ medicine and what matters in the end .pdf")
print("done adding the biology of cancer")

vectordb.add_to_vectorstore_from_pdf("data/PDF/The Emperor of All Maladies_ A Biography of Cancer.pdf")
print("done adding the biology of cancer")

print("finished vectorization")

vectordb_path = "VectorDB/vectorstore_mainV2"
cancer_app_instance = CancerApp(vectordb_path)

#Endpoint for text-to-text processing.
@flask_app.route('/text_to_text', methods=['POST'])
def api_text_to_text():
    data = request.get_json()
    message = data.get("message", "")
    saved_response = data.get("Saved_response", "")
    response, saved_response = cancer_app_instance.text_to_text(message, Saved_response=saved_response)
    return jsonify({"gemini_response": response, "Saved_response": saved_response})



# Endpoint for text-to-speech processing.
@flask_app.route('/text_to_speech', methods=['POST'])
def api_text_to_speech():
    data = request.get_json()
    message = data.get("message", "")
    output_filename = data.get("output_filename", "output.wav")
    saved_response = data.get("Saved_response", "")
    print("got the data")
    # Generate the WAV file from the text
    saved_response = cancer_app_instance.text_to_speech(message, output_filename, Saved_response=saved_response)
    # Return the file instead of JSON
    return send_file(output_filename, mimetype="audio/wav", as_attachment=True)

# Endpoint for speech-to-text processing.
@flask_app.route('/speech_to_text', methods=['POST'])
def api_speech_to_text():
    if 'audiofile' not in request.files:
        return jsonify({"error": "No audio file provided"}), 400
    audio_file = request.files['audiofile']
    # Save uploaded file temporarily.
    audio_ext = audio_file.filename.split('.')[-1]
    audio_path = f"temp_audio_input.{audio_ext}"
    audio_file.save(audio_path)
    saved_response = request.form.get("Saved_response", "")
    gemini_response, saved_response = cancer_app_instance.speech_to_text(audio_path, Saved_response=saved_response)
    os.remove(audio_path)
    return jsonify({"gemini_response": gemini_response, "Saved_response": saved_response})

# Endpoint for speech-to-speech processing.
@flask_app.route('/speech_to_speech', methods=['POST'])
def api_speech_to_speech():
    if 'audiofile' not in request.files:
        return jsonify({"error": "No audio file provided"}), 400
    audio_file = request.files['audiofile']
    audio_ext = audio_file.filename.split('.')[-1]
    audio_path = f"temp_audio_input.{audio_ext}"
    audio_file.save(audio_path)
    output_filename = request.form.get("output_filename", "speech_output.wav")
    saved_response = request.form.get("Saved_response", "")
    saved_response = cancer_app_instance.speech_to_speech(audio_path, output_filename, Saved_response=saved_response)
    os.remove(audio_path)
    # Return the output file for download.
    return send_file(output_filename, as_attachment=True)

if __name__ == '__main__':
    flask_app.run(host='0.0.0.0', port=7860, debug=True)