Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ from queue import Queue
|
|
11 |
# Global TTS queue
|
12 |
tts_queue = Queue()
|
13 |
|
14 |
-
#
|
15 |
data = None
|
16 |
|
17 |
def load_training_data():
|
@@ -26,31 +26,51 @@ def load_training_data():
|
|
26 |
company_name = row.get("اسم الشركه", "").strip()
|
27 |
date = row.get("تاريخ الدخول", "").strip()
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
40 |
except FileNotFoundError:
|
41 |
print("Warning: Data file not found. Starting with empty dataset.")
|
|
|
42 |
|
43 |
def parse_date(date_str):
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
def get_week_range(date_str):
|
|
|
47 |
current_date = parse_date(date_str)
|
|
|
|
|
|
|
48 |
days_to_subtract = (current_date.weekday() + 2) % 7
|
49 |
start_of_week = current_date - timedelta(days=days_to_subtract)
|
50 |
end_of_week = start_of_week + timedelta(days=6)
|
51 |
return start_of_week, end_of_week
|
52 |
|
53 |
def text_to_speech(text):
|
|
|
|
|
|
|
|
|
54 |
try:
|
55 |
# Create static directory if it doesn't exist
|
56 |
os.makedirs("static", exist_ok=True)
|
@@ -61,104 +81,166 @@ def text_to_speech(text):
|
|
61 |
speech.save(filepath)
|
62 |
return filepath
|
63 |
except Exception as e:
|
64 |
-
print("
|
65 |
return None
|
66 |
|
67 |
def calculate_weekly_total(date_str):
|
|
|
68 |
load_training_data()
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
72 |
return "Invalid date format. Please enter a date in the format dd.mm.yyyy."
|
|
|
73 |
start_of_week, end_of_week = get_week_range(date_str)
|
|
|
|
|
|
|
74 |
weekly_total = 0
|
75 |
for date_key in data.keys():
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
if "شغل" in report:
|
82 |
-
money_values = re.findall(r'شغل\s*(\d+)\s*شيكل', report)
|
83 |
-
else:
|
84 |
-
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
85 |
-
money_values = [int(value) for value in money_values]
|
86 |
-
weekly_total += sum(money_values)
|
87 |
-
except ValueError:
|
88 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
return weekly_total
|
90 |
|
91 |
def calculate_weekly_cash_total(date_str):
|
|
|
92 |
load_training_data()
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
96 |
return "Invalid date format. Please enter a date in the format dd.mm.yyyy."
|
|
|
97 |
start_of_week, end_of_week = get_week_range(date_str)
|
|
|
|
|
|
|
98 |
weekly_cash_total = 0
|
99 |
for date_key in data.keys():
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
if "كاش" in plate_number:
|
106 |
-
report = record.get("تقرير نهائي", "")
|
107 |
-
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
108 |
-
money_values = [int(value) for value in money_values]
|
109 |
-
weekly_cash_total += sum(money_values)
|
110 |
-
except ValueError:
|
111 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
return weekly_cash_total
|
113 |
|
114 |
def search_partial_matches(input_text):
|
|
|
115 |
load_training_data()
|
|
|
|
|
|
|
|
|
116 |
input_text = input_text.strip()
|
|
|
|
|
|
|
117 |
matching_records = {}
|
118 |
for key in data.keys():
|
119 |
-
if input_text in key:
|
120 |
matching_records[key] = data[key]
|
|
|
121 |
return matching_records
|
122 |
|
123 |
def calculate_total_for_period(start_date_str, end_date_str):
|
|
|
124 |
load_training_data()
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
129 |
return "Invalid date format. Please enter dates in the format dd.mm.yyyy."
|
|
|
130 |
total_amount = 0
|
131 |
for date_key in data.keys():
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
if "شغل" in report:
|
138 |
-
money_values = re.findall(r'شغل\s*(\d+)\s*شيكل', report)
|
139 |
-
else:
|
140 |
-
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
141 |
-
money_values = [int(value) for value in money_values]
|
142 |
-
total_amount += sum(money_values)
|
143 |
-
except ValueError:
|
144 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
return total_amount
|
146 |
|
147 |
def chatbot(input_text, start_date_str="", end_date_str="", enable_voice=False):
|
|
|
|
|
148 |
if start_date_str and end_date_str:
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
151 |
else:
|
152 |
return original_chatbot(input_text, enable_voice)
|
153 |
|
154 |
def original_chatbot(input_text, enable_voice):
|
|
|
|
|
|
|
|
|
155 |
load_training_data()
|
156 |
matching_records = search_partial_matches(input_text)
|
157 |
|
158 |
total_money = 0
|
159 |
filtered_records = {}
|
160 |
|
|
|
161 |
for key, records in matching_records.items():
|
|
|
|
|
162 |
filtered_records[key] = [info for info in records if "شيكل" in info.get("تقرير نهائي", "")]
|
163 |
|
164 |
res_list = []
|
@@ -167,21 +249,36 @@ def original_chatbot(input_text, enable_voice):
|
|
167 |
responses = []
|
168 |
|
169 |
for key, records in filtered_records.items():
|
170 |
-
if
|
|
|
|
|
|
|
|
|
171 |
company_name = records[0].get("اسم الشركه", "")
|
172 |
-
|
|
|
173 |
|
|
|
174 |
for info in records:
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
|
|
|
185 |
num_records_found = f"Number of records found: {len(responses)}"
|
186 |
total_money_str = f"Total Money: {total_money} شيكل"
|
187 |
combined_output = f"{num_records_found} - {total_money_str}"
|
@@ -191,18 +288,29 @@ def original_chatbot(input_text, enable_voice):
|
|
191 |
combined_output = "No matching entries found in the data."
|
192 |
response = ""
|
193 |
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
-
|
198 |
-
|
|
|
|
|
|
|
|
|
199 |
|
|
|
200 |
audio_file = None
|
201 |
-
if enable_voice:
|
202 |
audio_file = text_to_speech("\n".join(res_list))
|
203 |
|
204 |
return (combined_output, response, f"Weekly Total: {weekly_total} - Weekly Cash Total: {weekly_cash_total}", audio_file)
|
205 |
|
|
|
206 |
iface = gr.Interface(
|
207 |
fn=chatbot,
|
208 |
inputs=[
|
|
|
11 |
# Global TTS queue
|
12 |
tts_queue = Queue()
|
13 |
|
14 |
+
# Global data storage
|
15 |
data = None
|
16 |
|
17 |
def load_training_data():
|
|
|
26 |
company_name = row.get("اسم الشركه", "").strip()
|
27 |
date = row.get("تاريخ الدخول", "").strip()
|
28 |
|
29 |
+
# Store by plate number
|
30 |
+
if plate_number:
|
31 |
+
if plate_number not in data:
|
32 |
+
data[plate_number] = []
|
33 |
+
data[plate_number].append(row)
|
34 |
|
35 |
+
# Store by company name
|
36 |
+
if company_name:
|
37 |
+
if company_name not in data:
|
38 |
+
data[company_name] = []
|
39 |
+
data[company_name].append(row)
|
40 |
|
41 |
+
# Store by date only if it looks like a valid date
|
42 |
+
if date and re.match(r'\d{1,2}\.\d{1,2}\.\d{4}', date):
|
43 |
+
if date not in data:
|
44 |
+
data[date] = []
|
45 |
+
data[date].append(row)
|
46 |
except FileNotFoundError:
|
47 |
print("Warning: Data file not found. Starting with empty dataset.")
|
48 |
+
data = {} # Ensure data is initialized even if file is not found
|
49 |
|
50 |
def parse_date(date_str):
|
51 |
+
"""Parse date with error handling"""
|
52 |
+
try:
|
53 |
+
return datetime.strptime(date_str, "%d.%m.%Y")
|
54 |
+
except ValueError:
|
55 |
+
# Return None for invalid date formats
|
56 |
+
return None
|
57 |
|
58 |
def get_week_range(date_str):
|
59 |
+
"""Get week range with error handling"""
|
60 |
current_date = parse_date(date_str)
|
61 |
+
if current_date is None:
|
62 |
+
return None, None # Return None values if date parsing failed
|
63 |
+
|
64 |
days_to_subtract = (current_date.weekday() + 2) % 7
|
65 |
start_of_week = current_date - timedelta(days=days_to_subtract)
|
66 |
end_of_week = start_of_week + timedelta(days=6)
|
67 |
return start_of_week, end_of_week
|
68 |
|
69 |
def text_to_speech(text):
|
70 |
+
"""Convert text to speech with error handling"""
|
71 |
+
if not text:
|
72 |
+
return None
|
73 |
+
|
74 |
try:
|
75 |
# Create static directory if it doesn't exist
|
76 |
os.makedirs("static", exist_ok=True)
|
|
|
81 |
speech.save(filepath)
|
82 |
return filepath
|
83 |
except Exception as e:
|
84 |
+
print(f"Text-to-speech error: {str(e)}")
|
85 |
return None
|
86 |
|
87 |
def calculate_weekly_total(date_str):
|
88 |
+
"""Calculate weekly total with improved error handling"""
|
89 |
load_training_data()
|
90 |
+
|
91 |
+
# Check if date format is valid
|
92 |
+
input_date = parse_date(date_str)
|
93 |
+
if input_date is None:
|
94 |
return "Invalid date format. Please enter a date in the format dd.mm.yyyy."
|
95 |
+
|
96 |
start_of_week, end_of_week = get_week_range(date_str)
|
97 |
+
if start_of_week is None or end_of_week is None:
|
98 |
+
return "Invalid date format. Please enter a date in the format dd.mm.yyyy."
|
99 |
+
|
100 |
weekly_total = 0
|
101 |
for date_key in data.keys():
|
102 |
+
# Try to parse the key as a date
|
103 |
+
record_date = parse_date(date_key)
|
104 |
+
|
105 |
+
# Skip keys that aren't valid dates or are out of range
|
106 |
+
if record_date is None or not (start_of_week <= record_date <= end_of_week):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
continue
|
108 |
+
|
109 |
+
# Process matching dates
|
110 |
+
for record in data[date_key]:
|
111 |
+
try:
|
112 |
+
report = record.get("تقرير نهائي", "")
|
113 |
+
if "شغل" in report:
|
114 |
+
money_values = re.findall(r'شغل\s*(\d+)\s*شيكل', report)
|
115 |
+
else:
|
116 |
+
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
117 |
+
|
118 |
+
# Convert found values to integers with error handling
|
119 |
+
money_values = [int(value) for value in money_values if value.isdigit()]
|
120 |
+
weekly_total += sum(money_values)
|
121 |
+
except (ValueError, TypeError):
|
122 |
+
continue
|
123 |
+
|
124 |
return weekly_total
|
125 |
|
126 |
def calculate_weekly_cash_total(date_str):
|
127 |
+
"""Calculate weekly cash total with improved error handling"""
|
128 |
load_training_data()
|
129 |
+
|
130 |
+
# Check if date format is valid
|
131 |
+
input_date = parse_date(date_str)
|
132 |
+
if input_date is None:
|
133 |
return "Invalid date format. Please enter a date in the format dd.mm.yyyy."
|
134 |
+
|
135 |
start_of_week, end_of_week = get_week_range(date_str)
|
136 |
+
if start_of_week is None or end_of_week is None:
|
137 |
+
return "Invalid date format. Please enter a date in the format dd.mm.yyyy."
|
138 |
+
|
139 |
weekly_cash_total = 0
|
140 |
for date_key in data.keys():
|
141 |
+
# Try to parse the key as a date
|
142 |
+
record_date = parse_date(date_key)
|
143 |
+
|
144 |
+
# Skip keys that aren't valid dates or are out of range
|
145 |
+
if record_date is None or not (start_of_week <= record_date <= end_of_week):
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
continue
|
147 |
+
|
148 |
+
# Process matching dates
|
149 |
+
for record in data[date_key]:
|
150 |
+
try:
|
151 |
+
plate_number = record.get("رقم المركبة", "")
|
152 |
+
if "كاش" in plate_number:
|
153 |
+
report = record.get("تقرير نهائي", "")
|
154 |
+
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
155 |
+
money_values = [int(value) for value in money_values if value.isdigit()]
|
156 |
+
weekly_cash_total += sum(money_values)
|
157 |
+
except (ValueError, TypeError):
|
158 |
+
continue
|
159 |
+
|
160 |
return weekly_cash_total
|
161 |
|
162 |
def search_partial_matches(input_text):
|
163 |
+
"""Search for partial matches with error checking"""
|
164 |
load_training_data()
|
165 |
+
|
166 |
+
if not input_text or not isinstance(input_text, str):
|
167 |
+
return {}
|
168 |
+
|
169 |
input_text = input_text.strip()
|
170 |
+
if not input_text:
|
171 |
+
return {}
|
172 |
+
|
173 |
matching_records = {}
|
174 |
for key in data.keys():
|
175 |
+
if isinstance(key, str) and input_text in key:
|
176 |
matching_records[key] = data[key]
|
177 |
+
|
178 |
return matching_records
|
179 |
|
180 |
def calculate_total_for_period(start_date_str, end_date_str):
|
181 |
+
"""Calculate total for period with improved error handling"""
|
182 |
load_training_data()
|
183 |
+
|
184 |
+
# Check if date formats are valid
|
185 |
+
start_date = parse_date(start_date_str)
|
186 |
+
end_date = parse_date(end_date_str)
|
187 |
+
|
188 |
+
if start_date is None or end_date is None:
|
189 |
return "Invalid date format. Please enter dates in the format dd.mm.yyyy."
|
190 |
+
|
191 |
total_amount = 0
|
192 |
for date_key in data.keys():
|
193 |
+
# Try to parse the key as a date
|
194 |
+
record_date = parse_date(date_key)
|
195 |
+
|
196 |
+
# Skip keys that aren't valid dates or are out of range
|
197 |
+
if record_date is None or not (start_date <= record_date <= end_date):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
continue
|
199 |
+
|
200 |
+
# Process matching dates
|
201 |
+
for record in data[date_key]:
|
202 |
+
try:
|
203 |
+
report = record.get("تقرير نهائي", "")
|
204 |
+
if "شغل" in report:
|
205 |
+
money_values = re.findall(r'شغل\s*(\d+)\s*شيكل', report)
|
206 |
+
else:
|
207 |
+
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
208 |
+
|
209 |
+
# Convert found values to integers with error handling
|
210 |
+
money_values = [int(value) for value in money_values if value.isdigit()]
|
211 |
+
total_amount += sum(money_values)
|
212 |
+
except (ValueError, TypeError):
|
213 |
+
continue
|
214 |
+
|
215 |
return total_amount
|
216 |
|
217 |
def chatbot(input_text, start_date_str="", end_date_str="", enable_voice=False):
|
218 |
+
"""Main chatbot function with error handling"""
|
219 |
+
# Handle date range query if provided
|
220 |
if start_date_str and end_date_str:
|
221 |
+
try:
|
222 |
+
total_for_period = calculate_total_for_period(start_date_str, end_date_str)
|
223 |
+
return (f"Total amount from {start_date_str} to {end_date_str}: {total_for_period} شيكل", "", "", None)
|
224 |
+
except Exception as e:
|
225 |
+
return (f"Error: {str(e)}", "", "", None)
|
226 |
else:
|
227 |
return original_chatbot(input_text, enable_voice)
|
228 |
|
229 |
def original_chatbot(input_text, enable_voice):
|
230 |
+
"""Original chatbot implementation with improved error handling"""
|
231 |
+
if not input_text or not isinstance(input_text, str):
|
232 |
+
return ("Please enter a valid query.", "", "", None)
|
233 |
+
|
234 |
load_training_data()
|
235 |
matching_records = search_partial_matches(input_text)
|
236 |
|
237 |
total_money = 0
|
238 |
filtered_records = {}
|
239 |
|
240 |
+
# Filter records containing "شيكل"
|
241 |
for key, records in matching_records.items():
|
242 |
+
if not records:
|
243 |
+
continue
|
244 |
filtered_records[key] = [info for info in records if "شيكل" in info.get("تقرير نهائي", "")]
|
245 |
|
246 |
res_list = []
|
|
|
249 |
responses = []
|
250 |
|
251 |
for key, records in filtered_records.items():
|
252 |
+
if not records:
|
253 |
+
continue
|
254 |
+
|
255 |
+
# Handle company name display
|
256 |
+
if key == "رقم المركبة" and records:
|
257 |
company_name = records[0].get("اسم الشركه", "")
|
258 |
+
if company_name:
|
259 |
+
res_list.append(f"اسم الشركة هو: {company_name}")
|
260 |
|
261 |
+
# Process each record
|
262 |
for info in records:
|
263 |
+
try:
|
264 |
+
# Format record details
|
265 |
+
response = "\n".join([f"{k}: {v}" for k, v in info.items() if v])
|
266 |
+
responses.append(response)
|
267 |
+
|
268 |
+
# Parse money values
|
269 |
+
report = info.get("تقرير نهائي", "")
|
270 |
+
if "شغل" in report:
|
271 |
+
money_values = re.findall(r'شغل\s*(\d+)\s*شيكل', report)
|
272 |
+
else:
|
273 |
+
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
274 |
+
|
275 |
+
# Convert to integers with error handling
|
276 |
+
money_values = [int(value) for value in money_values if value.isdigit()]
|
277 |
+
total_money += sum(money_values)
|
278 |
+
except (ValueError, TypeError):
|
279 |
+
continue
|
280 |
|
281 |
+
# Format results
|
282 |
num_records_found = f"Number of records found: {len(responses)}"
|
283 |
total_money_str = f"Total Money: {total_money} شيكل"
|
284 |
combined_output = f"{num_records_found} - {total_money_str}"
|
|
|
288 |
combined_output = "No matching entries found in the data."
|
289 |
response = ""
|
290 |
|
291 |
+
# Calculate weekly totals
|
292 |
+
try:
|
293 |
+
weekly_total = calculate_weekly_total(input_text)
|
294 |
+
res_list.append(f"مجموع الدخل الأسبوعي هو: {weekly_total}")
|
295 |
+
except Exception as e:
|
296 |
+
weekly_total = f"Error calculating weekly total: {str(e)}"
|
297 |
+
res_list.append(f"Error: {str(e)}")
|
298 |
|
299 |
+
try:
|
300 |
+
weekly_cash_total = calculate_weekly_cash_total(input_text)
|
301 |
+
res_list.append(f"مجموع الكاش المقبوض في هذا الاسبوع هو: {weekly_cash_total}")
|
302 |
+
except Exception as e:
|
303 |
+
weekly_cash_total = f"Error calculating weekly cash total: {str(e)}"
|
304 |
+
res_list.append(f"Error: {str(e)}")
|
305 |
|
306 |
+
# Generate audio if enabled
|
307 |
audio_file = None
|
308 |
+
if enable_voice and res_list:
|
309 |
audio_file = text_to_speech("\n".join(res_list))
|
310 |
|
311 |
return (combined_output, response, f"Weekly Total: {weekly_total} - Weekly Cash Total: {weekly_cash_total}", audio_file)
|
312 |
|
313 |
+
# Define the Gradio interface
|
314 |
iface = gr.Interface(
|
315 |
fn=chatbot,
|
316 |
inputs=[
|