File size: 12,663 Bytes
c5ecd72
 
 
 
 
 
d940f83
c5ecd72
 
 
d940f83
c5ecd72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d940f83
c5ecd72
 
 
 
 
d940f83
 
c5ecd72
 
d940f83
 
c5ecd72
 
 
 
 
 
 
 
 
d940f83
c5ecd72
 
 
 
 
 
 
 
d940f83
c5ecd72
8981e66
c5ecd72
 
 
8981e66
c5ecd72
 
 
 
 
8981e66
c5ecd72
8981e66
c5ecd72
 
 
 
 
 
8981e66
c5ecd72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d940f83
 
c5ecd72
 
 
 
 
8981e66
c5ecd72
 
 
d940f83
c5ecd72
 
 
 
d940f83
c5ecd72
 
 
 
d940f83
c5ecd72
 
 
 
 
 
 
d940f83
c5ecd72
 
 
 
 
d940f83
c5ecd72
 
d940f83
c5ecd72
d940f83
c5ecd72
 
 
 
 
 
 
 
 
 
 
 
 
d940f83
c5ecd72
 
 
 
 
 
d940f83
c5ecd72
 
d940f83
c5ecd72
51265e9
c5ecd72
d940f83
c5ecd72
d940f83
c5ecd72
801fe7c
d940f83
c5ecd72
 
 
 
d940f83
 
 
c5ecd72
 
 
 
 
d940f83
c5ecd72
 
 
d940f83
 
 
c5ecd72
 
 
1412372
c5ecd72
 
 
 
 
 
 
4d6e31c
1412372
 
 
 
 
 
 
 
c5ecd72
 
d940f83
af539aa
 
 
 
c5ecd72
d3d58e4
c5ecd72
d3d58e4
d940f83
c5ecd72
 
bc676ba
c5ecd72
ae2daab
c5ecd72
bde5081
c5ecd72
 
 
 
 
 
 
 
bde5081
c5ecd72
4e5f5cf
 
 
c5ecd72
4e5f5cf
c5ecd72
bc676ba
c5ecd72
 
 
 
c395c47
 
af539aa
 
 
 
 
 
 
 
 
 
 
 
 
c5ecd72
bc676ba
af539aa
 
 
 
 
 
 
 
 
 
c5ecd72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c82f40
 
c5ecd72
0c82f40
c5ecd72
 
 
c0fe2cc
c5ecd72
 
 
8981e66
c0fe2cc
c5ecd72
 
dc80dbe
c5ecd72
 
 
 
 
dc80dbe
 
07a8fb8
 
 
 
 
 
dc80dbe
 
 
 
 
 
 
 
af539aa
 
 
c395c47
7608a3e
4d6e31c
af539aa
 
7608a3e
29f410d
3bbb164
af539aa
 
 
 
 
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# import torch
# import asyncio
# import logging
# import signal
# import uvicorn
# import os 

# from fastapi import FastAPI, Request, HTTPException, status
# from pydantic import BaseModel, Field
# from langdetect import detect

# from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, GenerationConfig
# from langchain.vectorstores import Qdrant
# from langchain.embeddings import HuggingFaceEmbeddings
# from langchain.chains import RetrievalQA
# from langchain.llms import HuggingFacePipeline
# from qdrant_client import QdrantClient
# from langchain.callbacks.base import BaseCallbackHandler
# from huggingface_hub import hf_hub_download
# from contextlib import asynccontextmanager

# # Get environment variables
# COLLECTION_NAME = "arabic_rag_collection"
# QDRANT_URL = os.getenv("QDRANT_URL", "https://12efeef2-9f10-4402-9deb-f070977ddfc8.eu-central-1-0.aws.cloud.qdrant.io:6333")
# QDRANT_API_KEY = os.getenv("QDRANT_API_KEY", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIn0.Jb39rYQW2rSE9RdXrjdzKY6T1RF44XjdQzCvzFkjat4")

# # === LOGGING === #
# logging.basicConfig(level=logging.DEBUG)
# logger = logging.getLogger(__name__)

# # Load model and tokenizer
# model_name = "FreedomIntelligence/Apollo-2B"
# tokenizer = AutoTokenizer.from_pretrained(model_name)
# model = AutoModelForCausalLM.from_pretrained(model_name)
# tokenizer.pad_token = tokenizer.eos_token


# # FastAPI setup
# app = FastAPI(title="Apollo RAG Medical Chatbot")


# # Generation settings
# generation_config = GenerationConfig(
#     max_new_tokens=150,
#     temperature=0.2,
#     top_k=20,
#     do_sample=True,
#     top_p=0.7,
#     repetition_penalty=1.3,
# )

# # Text generation pipeline
# llm_pipeline = pipeline(
#     model=model,
#     tokenizer=tokenizer,
#     task="text-generation",
#     generation_config=generation_config,
#     device=model.device.index if model.device.type == "cuda" else -1
# )

# llm = HuggingFacePipeline(pipeline=llm_pipeline)

# # Connect to Qdrant + embedding
# embedding = HuggingFaceEmbeddings(model_name="Omartificial-Intelligence-Space/GATE-AraBert-v1")
# qdrant_client = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)

# vector_store = Qdrant(
#     client=qdrant_client,
#     collection_name=COLLECTION_NAME,
#     embeddings=embedding
# )

# retriever = vector_store.as_retriever(search_kwargs={"k": 3})

# # Set up RAG QA chain
# qa_chain = RetrievalQA.from_chain_type(
#     llm=llm,
#     retriever=retriever,
#     chain_type="stuff"
# )

# class Query(BaseModel):
#     question: str = Field(..., example="ما هي اسباب تساقط الشعر ؟", min_length=3)

# class TimeoutCallback(BaseCallbackHandler):
#     def __init__(self, timeout_seconds: int = 60):
#         self.timeout_seconds = timeout_seconds
#         self.start_time = None

#     async def on_llm_start(self, *args, **kwargs):
#         self.start_time = asyncio.get_event_loop().time()

#     async def on_llm_new_token(self, *args, **kwargs):
#         if asyncio.get_event_loop().time() - self.start_time > self.timeout_seconds:
#             raise TimeoutError("LLM processing timeout")


# # def generate_prompt(question: str) -> str:
# #     lang = detect(question)
# #     if lang == "ar":
# #         return (
# #             "أجب على السؤال الطبي التالي بلغة عربية فصحى، بإجابة دقيقة ومفصلة. إذا لم تجد معلومات كافية في السياق، استخدم معرفتك الطبية السابقة. \n"
# #             "- عدم تكرار أي نقطة أو عبارة أو كلمة\n"
# #             "- وضوح وسلاسة كل نقطة\n"
# #             "- تجنب الحشو والعبارات الزائدة\n"
# #             f"\nالسؤال: {question}\nالإجابة:"
# #         )
# #     else:
# #         return (
# #             "Answer the following medical question in clear English with a detailed, non-redundant response. "
# #             "Do not repeat ideas, phrases, or restate the question in the answer. If the context lacks relevant "
# #             "information, rely on your prior medical knowledge. If the answer involves multiple points, list them "
# #             "in concise and distinct bullet points:\n"
# #             f"Question: {question}\nAnswer:"
# #         )

# def generate_prompt(question):
#     lang = detect(question)
#     if lang == "ar":
#         return f"""أجب على السؤال الطبي التالي بلغة عربية فصحى، بإجابة دقيقة ومفصلة. إذا لم تجد معلومات كافية في السياق، استخدم معرفتك الطبية السابقة. 
#  وتأكد من ان:
# - عدم تكرار أي نقطة أو عبارة أو كلمة
# - وضوح وسلاسة كل نقطة
# - تجنب الحشو والعبارات الزائدة-

# السؤال: {question}
# الإجابة:
# """
        
#     else:
#         return f"""Answer the following medical question in clear English with a detailed, non-redundant response. Do not repeat ideas, phrases, or restate the question in the answer. If the context lacks relevant information, rely on your prior medical knowledge. If the answer involves multiple points, list them in concise and distinct bullet points:
# Question: {question}
# Answer:"""

# # === ROUTES === #
# @app.get("/")
# async def root():
#     return {"message": "Medical QA API is running!"}

# @app.post("/ask")
# async def ask(query: Query):
#     try:
#         logger.debug(f"Received question: {query.question}")
#         prompt = generate_prompt(query.question)
#         timeout_callback = TimeoutCallback(timeout_seconds=360)
#         loop = asyncio.get_event_loop()
        
#         response = await asyncio.wait_for(
#             # qa_chain.run(prompt, callbacks=[timeout_callback]),
#             loop.run_in_executor(None, qa_chain.run, prompt),
#             timeout=360
#         )

#         if not response:
#             raise ValueError("Empty answer returned from model")

#         answer = response.split("Answer:")[-1].strip() if "Answer:" in response else response.split("الإجابة:")[-1].strip()
        
#         return {
#             "status": "success",
#             "response": response,
#             "answer": answer,
#             "language": detect(query.question)
#         }

#     except TimeoutError as te:
#         logger.error("Request timed out", exc_info=True)
#         raise HTTPException(
#             status_code=status.HTTP_504_GATEWAY_TIMEOUT,
#             detail={"status": "error", "message": "Request timed out", "error": str(te)}
#         )

#     except Exception as e:
#         logger.error(f"Unexpected error: {e}", exc_info=True)
#         raise HTTPException(
#             status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
#             detail={"status": "error", "message": "Internal server error", "error": str(e)}
#         )

# @app.post("/chat")
# def chat(query: Query):

#     logger.debug(f"Received question: {query.question}")

#     prompt = generate_prompt(query.question)

#     response = qa_chain.run(prompt)

#     answer = response.split("Answer:")[-1].strip() if "Answer:" in response else response.split("الإجابة:")[-1].strip()
    

#     return {
#         "response": response,
#         "answer": answer
#     }

    

# # === ENTRYPOINT === #
# if __name__ == "__main__":
#     def handle_exit(signum, frame):
#         print("Shutting down gracefully...")
#         exit(0)

#     signal.signal(signal.SIGINT, handle_exit)
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=8000)



from langdetect import detect
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline, GenerationConfig
import torch
import os
import logging
from fastapi import FastAPI, Request, HTTPException, status
from pydantic import BaseModel, Field
import time
import asyncio
from concurrent.futures import ThreadPoolExecutor
from fastapi.middleware.cors import CORSMiddleware

from langchain.vectorstores import Qdrant
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.chains import RetrievalQA
from langchain.llms import HuggingFacePipeline
from qdrant_client import QdrantClient



logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

COLLECTION_NAME = "arabic_rag_collection"
QDRANT_URL = os.getenv("QDRANT_URL", "https://12efeef2-9f10-4402-9deb-f070977ddfc8.eu-central-1-0.aws.cloud.qdrant.io:6333")
QDRANT_API_KEY = os.getenv("QDRANT_API_KEY", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIn0.Jb39rYQW2rSE9RdXrjdzKY6T1RF44XjdQzCvzFkjat4")

# Load model and tokenizer
# model_name = "FreedomIntelligence/Apollo-7B"
# model_name = "emilyalsentzer/Bio_ClinicalBERT"
model_name = "FreedomIntelligence/Apollo-2B"

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

tokenizer.pad_token = tokenizer.eos_token

app = FastAPI(title="Apollo RAG Medical Chatbot")

# Add this after creating the `app`
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allow all origins
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

generation_config = GenerationConfig(
    max_new_tokens=200,
    temperature=0.3,
    top_k=50,
    do_sample=True,
    top_p=0.9,
)

# Create generation pipeline
pipe = TextGenerationPipeline(
    model=model,
    tokenizer=tokenizer,
    generation_config = generation_config,
    task = "text-generation",
    device=model.device.index if torch.cuda.is_available() else -1
)

llm = HuggingFacePipeline(pipeline=pipe)

embedding = HuggingFaceEmbeddings(model_name="Omartificial-Intelligence-Space/GATE-AraBert-v1")

qdrant_client = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)

vector_store = Qdrant(
    client=qdrant_client,
    collection_name=COLLECTION_NAME,
    embeddings=embedding
)

retriever = vector_store.as_retriever(search_kwargs={"k": 3})

# ----------------- RAG Chain ------------------
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    chain_type="stuff"
)


# Prompt formatter based on language
def generate_prompt(message):
    lang = detect(message)
    if lang == "ar":
        return f"""أجب على السؤال الطبي التالي بلغة عربية فصحى، بإجابة دقيقة ومفصلة. إذا لم تجد معلومات كافية في السياق، استخدم معرفتك الطبية السابقة.
 وتأكد من ان:
- عدم تكرار أي نقطة أو عبارة أو كلمة
- وضوح وسلاسة كل نقطة
- تجنب الحشو والعبارات الزائدة
السؤال: {message}
الإجابة:"""
    else:
        return f"""Answer the following medical question in clear English with a detailed, non-redundant response. Do not repeat ideas or restate the question. If information is missing, rely on your prior medical knowledge:
Question: {message}
Answer:"""


executor = ThreadPoolExecutor()

# Define request model
class Query(BaseModel):
    message: str

@app.get("/")
def read_root():
    return {"message": "Apollo Medical Chatbot API is running"}


@app.post("/ask")
async def chat_fn(query: Query):
    
    message = query.message
    logger.info(f"Received message: {message}")
    
    prompt = generate_prompt(message)

    # Run blocking inference in thread
    loop = asyncio.get_event_loop()
    response = await loop.run_in_executor(executor, lambda: pipe(prompt,
                                                                 max_new_tokens=512,
                                                                 temperature=0.7,
                                                                 do_sample=True,
                                                                 top_p=0.9)[0]['generated_text'])


    # Parse answer
    answer = response.split("Answer:")[-1].strip() if "Answer:" in response else response.split("الإجابة:")[-1].strip()
    return {
        "response": response,
        "Answer": answer
    }

@app.post("/ask-rag")
async def chat_fn(query: Query):
    message = query.message
    prompt = generate_prompt(message)
    logger.info(f"Received message: {message}")

    # Run RAG inference in thread
    loop = asyncio.get_event_loop()
    response = await loop.run_in_executor(executor, lambda: qa_chain.run(prompt))

    answer = response.split("Answer:")[-1].strip() if "Answer:" in response else response.split("الإجابة:")[-1].strip()
    
    return {
        "response": response,
        "answer": answer
    }