deveix commited on
Commit
8b248fd
·
1 Parent(s): b4cb1c3

add ayah info

Browse files
Files changed (2) hide show
  1. app/main.py +17 -5
  2. requirements.txt +1 -0
app/main.py CHANGED
@@ -8,6 +8,7 @@ import uvicorn
8
  from dotenv import load_dotenv
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from uuid import uuid4
 
11
 
12
  import joblib
13
  import librosa
@@ -112,8 +113,18 @@ def index_file(filepath):
112
 
113
  return index
114
 
 
 
 
 
 
 
 
 
 
 
115
 
116
- def get_text_by_block_number(filepath, block_numbers):
117
  """ Retrieve specific blocks from a file based on block numbers, where each block is separated by '\n\n'. """
118
  blocks_text = []
119
  with open(filepath, 'r', encoding='utf-8') as file:
@@ -127,11 +138,12 @@ def get_text_by_block_number(filepath, block_numbers):
127
  ayah = splitted[0]
128
  tafsir = splitted[1]
129
  # Replace single newlines within blocks with space and strip leading/trailing whitespace
130
- formatted_block = ' '.join(splitted).strip()
131
  blocks_text.append({
132
- ayah:ayah,
133
- tafsir:tafsir,
134
- block:formatted_block
 
135
  })
136
  if len(blocks_text) == len(block_numbers): # Stop reading once all required blocks are retrieved
137
  break
 
8
  from dotenv import load_dotenv
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from uuid import uuid4
11
+ import httpx
12
 
13
  import joblib
14
  import librosa
 
113
 
114
  return index
115
 
116
+ async def get_ayah_info(ayah):
117
+ """Asynchronously fetches Ayah information from the Al-Quran API."""
118
+ url = f"https://api.alquran.cloud/v1/search/{ayah}/all/ar"
119
+ async with httpx.AsyncClient() as client:
120
+ response = await client.get(url)
121
+ if response.status_code == 200:
122
+ return response.json()
123
+ else:
124
+ return {"error": "Failed to fetch data"}
125
+
126
 
127
+ async def get_text_by_block_number(filepath, block_numbers):
128
  """ Retrieve specific blocks from a file based on block numbers, where each block is separated by '\n\n'. """
129
  blocks_text = []
130
  with open(filepath, 'r', encoding='utf-8') as file:
 
138
  ayah = splitted[0]
139
  tafsir = splitted[1]
140
  # Replace single newlines within blocks with space and strip leading/trailing whitespace
141
+ ayah_info = await get_ayah_info(ayah) # This makes the API call
142
  blocks_text.append({
143
+ "ayah": ayah.replace(" ", ''),
144
+ "tafsir": tafsir,
145
+ "ayah_info": ayah_info
146
+ # "block": formatted_block
147
  })
148
  if len(blocks_text) == len(block_numbers): # Stop reading once all required blocks are retrieved
149
  break
requirements.txt CHANGED
@@ -20,3 +20,4 @@ python-multipart
20
  ffmpeg-python
21
  noisereduce
22
  scikit-learn==1.2.2
 
 
20
  ffmpeg-python
21
  noisereduce
22
  scikit-learn==1.2.2
23
+ httpx