Upload folder using huggingface_hub
Browse files- README.md +3 -3
- app.py +249 -0
- requirements.txt +3 -4
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Khanfar
|
3 |
-
app_file:
|
4 |
sdk: gradio
|
5 |
-
sdk_version:
|
6 |
-
---
|
|
|
1 |
---
|
2 |
title: Khanfar
|
3 |
+
app_file: app.py
|
4 |
sdk: gradio
|
5 |
+
sdk_version: 3.50.2
|
6 |
+
---
|
app.py
ADDED
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import threading
|
2 |
+
import time
|
3 |
+
import gradio as gr
|
4 |
+
import csv
|
5 |
+
import re
|
6 |
+
import os
|
7 |
+
from datetime import datetime, timedelta
|
8 |
+
from gtts import gTTS
|
9 |
+
from queue import Queue
|
10 |
+
|
11 |
+
# Global TTS queue
|
12 |
+
tts_queue = Queue()
|
13 |
+
|
14 |
+
# Load data from the CSV file when the application starts
|
15 |
+
data = None
|
16 |
+
|
17 |
+
def load_training_data():
|
18 |
+
global data
|
19 |
+
if data is None:
|
20 |
+
data = {}
|
21 |
+
try:
|
22 |
+
with open("docs/your_data.csv", "r", encoding="utf-8-sig") as csvfile:
|
23 |
+
reader = csv.DictReader(csvfile)
|
24 |
+
for row in reader:
|
25 |
+
plate_number = row.get("رقم المركبة", "").strip()
|
26 |
+
company_name = row.get("اسم الشركه", "").strip()
|
27 |
+
date = row.get("تاريخ الدخول", "").strip()
|
28 |
+
|
29 |
+
if plate_number not in data:
|
30 |
+
data[plate_number] = []
|
31 |
+
data[plate_number].append(row)
|
32 |
+
|
33 |
+
if company_name not in data:
|
34 |
+
data[company_name] = []
|
35 |
+
data[company_name].append(row)
|
36 |
+
|
37 |
+
if date not in data:
|
38 |
+
data[date] = []
|
39 |
+
data[date].append(row)
|
40 |
+
except FileNotFoundError:
|
41 |
+
print("Warning: Data file not found. Starting with empty dataset.")
|
42 |
+
# Create a placeholder data structure
|
43 |
+
data = {"example_plate": [], "example_company": [], "01.01.2025": []}
|
44 |
+
|
45 |
+
def parse_date(date_str):
|
46 |
+
try:
|
47 |
+
return datetime.strptime(date_str, "%d.%m.%Y")
|
48 |
+
except (ValueError, TypeError):
|
49 |
+
# Return a default date if parsing fails
|
50 |
+
return datetime.now()
|
51 |
+
|
52 |
+
def get_week_range(date_str):
|
53 |
+
current_date = parse_date(date_str)
|
54 |
+
days_to_subtract = (current_date.weekday() + 2) % 7
|
55 |
+
start_of_week = current_date - timedelta(days=days_to_subtract)
|
56 |
+
end_of_week = start_of_week + timedelta(days=6)
|
57 |
+
return start_of_week, end_of_week
|
58 |
+
|
59 |
+
def text_to_speech(text):
|
60 |
+
try:
|
61 |
+
# Create static directory if it doesn't exist
|
62 |
+
os.makedirs("static", exist_ok=True)
|
63 |
+
|
64 |
+
speech = gTTS(text=text, lang='ar', slow=False)
|
65 |
+
filename = f"text_to_speech_{int(time.time())}.mp3"
|
66 |
+
filepath = os.path.join("static", filename)
|
67 |
+
speech.save(filepath)
|
68 |
+
return filepath
|
69 |
+
except Exception as e:
|
70 |
+
print("Exception:", str(e))
|
71 |
+
return None
|
72 |
+
|
73 |
+
def calculate_weekly_total(date_str):
|
74 |
+
load_training_data()
|
75 |
+
try:
|
76 |
+
input_date = parse_date(date_str)
|
77 |
+
except ValueError:
|
78 |
+
return "Invalid date format. Please enter a date in the format dd.mm.yyyy."
|
79 |
+
start_of_week, end_of_week = get_week_range(date_str)
|
80 |
+
weekly_total = 0
|
81 |
+
|
82 |
+
for date_key in data.keys():
|
83 |
+
try:
|
84 |
+
record_date = parse_date(date_key)
|
85 |
+
if start_of_week <= record_date <= end_of_week:
|
86 |
+
for record in data[date_key]:
|
87 |
+
report = record.get("تقرير نهائي", "")
|
88 |
+
if "شغل" in report:
|
89 |
+
money_values = re.findall(r'شغل\s*(\d+)\s*شيكل', report)
|
90 |
+
else:
|
91 |
+
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
92 |
+
money_values = [int(value) for value in money_values]
|
93 |
+
weekly_total += sum(money_values)
|
94 |
+
except ValueError:
|
95 |
+
continue
|
96 |
+
return weekly_total
|
97 |
+
|
98 |
+
def calculate_weekly_cash_total(date_str):
|
99 |
+
load_training_data()
|
100 |
+
try:
|
101 |
+
input_date = parse_date(date_str)
|
102 |
+
except ValueError:
|
103 |
+
return "Invalid date format. Please enter a date in the format dd.mm.yyyy."
|
104 |
+
start_of_week, end_of_week = get_week_range(date_str)
|
105 |
+
weekly_cash_total = 0
|
106 |
+
|
107 |
+
for date_key in data.keys():
|
108 |
+
try:
|
109 |
+
record_date = parse_date(date_key)
|
110 |
+
if start_of_week <= record_date <= end_of_week:
|
111 |
+
for record in data[date_key]:
|
112 |
+
plate_number = record.get("رقم المركبة", "")
|
113 |
+
if "كاش" in plate_number:
|
114 |
+
report = record.get("تقرير نهائي", "")
|
115 |
+
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
116 |
+
money_values = [int(value) for value in money_values]
|
117 |
+
weekly_cash_total += sum(money_values)
|
118 |
+
except ValueError:
|
119 |
+
continue
|
120 |
+
return weekly_cash_total
|
121 |
+
|
122 |
+
def search_partial_matches(input_text):
|
123 |
+
load_training_data()
|
124 |
+
input_text = input_text.strip()
|
125 |
+
matching_records = {}
|
126 |
+
|
127 |
+
for key in data.keys():
|
128 |
+
if input_text in key:
|
129 |
+
matching_records[key] = data[key]
|
130 |
+
return matching_records
|
131 |
+
|
132 |
+
def calculate_total_for_period(start_date_str, end_date_str):
|
133 |
+
load_training_data()
|
134 |
+
try:
|
135 |
+
start_date = parse_date(start_date_str)
|
136 |
+
end_date = parse_date(end_date_str)
|
137 |
+
except ValueError:
|
138 |
+
return "Invalid date format. Please enter dates in the format dd.mm.yyyy."
|
139 |
+
|
140 |
+
total_amount = 0
|
141 |
+
for date_key in data.keys():
|
142 |
+
try:
|
143 |
+
record_date = parse_date(date_key)
|
144 |
+
if start_date <= record_date <= end_date:
|
145 |
+
for record in data[date_key]:
|
146 |
+
report = record.get("تقرير نهائي", "")
|
147 |
+
if "شغل" in report:
|
148 |
+
money_values = re.findall(r'شغل\s*(\d+)\s*شيكل', report)
|
149 |
+
else:
|
150 |
+
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
151 |
+
money_values = [int(value) for value in money_values]
|
152 |
+
total_amount += sum(money_values)
|
153 |
+
except ValueError:
|
154 |
+
continue
|
155 |
+
return total_amount
|
156 |
+
|
157 |
+
def chatbot(input_text, start_date_str="", end_date_str="", enable_voice=False):
|
158 |
+
if start_date_str and end_date_str:
|
159 |
+
total_for_period = calculate_total_for_period(start_date_str, end_date_str)
|
160 |
+
return (f"Total amount from {start_date_str} to {end_date_str}: {total_for_period} شيكل", "", "", None)
|
161 |
+
else:
|
162 |
+
return original_chatbot(input_text, enable_voice)
|
163 |
+
|
164 |
+
def original_chatbot(input_text, enable_voice):
|
165 |
+
load_training_data()
|
166 |
+
matching_records = search_partial_matches(input_text)
|
167 |
+
|
168 |
+
total_money = 0
|
169 |
+
filtered_records = {}
|
170 |
+
|
171 |
+
for key, records in matching_records.items():
|
172 |
+
filtered_records[key] = [info for info in records if "شيكل" in info.get("تقرير نهائي", "")]
|
173 |
+
|
174 |
+
res_list = []
|
175 |
+
|
176 |
+
if filtered_records:
|
177 |
+
responses = []
|
178 |
+
|
179 |
+
for key, records in filtered_records.items():
|
180 |
+
if key == "رقم المركبة":
|
181 |
+
company_name = records[0].get("اسم الشركه", "")
|
182 |
+
res_list.append(f"اسم الشركة هو: {company_name}")
|
183 |
+
|
184 |
+
for info in records:
|
185 |
+
response = "\n".join([f"{key}: {value}" for key, value in info.items()])
|
186 |
+
responses.append(response)
|
187 |
+
report = info.get("تقرير نهائي", "")
|
188 |
+
if "شغل" in report:
|
189 |
+
money_values = re.findall(r'شغل\s*(\d+)\s*شيكل', report)
|
190 |
+
else:
|
191 |
+
money_values = re.findall(r'(\d+)\s*شيكل', report)
|
192 |
+
money_values = [int(value) for value in money_values]
|
193 |
+
total_money += sum(money_values)
|
194 |
+
|
195 |
+
num_records_found = f"Number of records found: {len(responses)}"
|
196 |
+
total_money_str = f"Total Money: {total_money} شيكل"
|
197 |
+
combined_output = f"{num_records_found} - {total_money_str}"
|
198 |
+
response = "\n\n---\n\n".join(responses)
|
199 |
+
res_list.append(f"مجموع الدخل اليومي في هذا اليوم هو: {total_money}")
|
200 |
+
else:
|
201 |
+
combined_output = "No matching entries found in the data."
|
202 |
+
response = ""
|
203 |
+
|
204 |
+
weekly_total = calculate_weekly_total(input_text)
|
205 |
+
res_list.append(f"مجموع الدخل الأسبوعي هو: {weekly_total}")
|
206 |
+
|
207 |
+
weekly_cash_total = calculate_weekly_cash_total(input_text)
|
208 |
+
res_list.append(f"مجموع الكاش المقبوض في هذا الاسبوع هو: {weekly_cash_total}")
|
209 |
+
|
210 |
+
audio_file = None
|
211 |
+
if enable_voice:
|
212 |
+
audio_file = text_to_speech("\n".join(res_list))
|
213 |
+
|
214 |
+
return (combined_output, response, f"Weekly Total: {weekly_total} - Weekly Cash Total: {weekly_cash_total}", audio_file)
|
215 |
+
|
216 |
+
# Using blocks for more compatibility
|
217 |
+
def create_interface():
|
218 |
+
with gr.Blocks(title="شركه ابناء عرفات") as interface:
|
219 |
+
gr.Markdown("# شركه ابناء عرفات")
|
220 |
+
gr.Markdown("بحث حسب اسم الشركه - التاريخ - نمره الشاحنه")
|
221 |
+
|
222 |
+
input_text = gr.Textbox(lines=2, placeholder="Enter Date or Company Name or Plate Number...")
|
223 |
+
start_date = gr.Textbox(lines=1, placeholder="بحث من تاريخ (dd.mm.yyyy)", label="بحث من تاريخ")
|
224 |
+
end_date = gr.Textbox(lines=1, placeholder="الى تاريخ (dd.mm.yyyy)", label="الى تاريخ")
|
225 |
+
enable_voice = gr.Checkbox(label="تفعيل الصوت")
|
226 |
+
|
227 |
+
submit_btn = gr.Button("بحث")
|
228 |
+
|
229 |
+
daily_income = gr.Textbox(label="مجموع الدخل اليومي")
|
230 |
+
reports = gr.Textbox(label="عرض التقارير")
|
231 |
+
weekly_income = gr.Textbox(label="مجموع الدخل الاسبوعي")
|
232 |
+
voice_output = gr.Audio(label="Play Voice")
|
233 |
+
|
234 |
+
submit_btn.click(
|
235 |
+
fn=chatbot,
|
236 |
+
inputs=[input_text, start_date, end_date, enable_voice],
|
237 |
+
outputs=[daily_income, reports, weekly_income, voice_output]
|
238 |
+
)
|
239 |
+
|
240 |
+
return interface
|
241 |
+
|
242 |
+
# Initialize the application
|
243 |
+
if __name__ == "__main__":
|
244 |
+
# Load data at startup
|
245 |
+
load_training_data()
|
246 |
+
|
247 |
+
# Create and launch the interface
|
248 |
+
demo = create_interface()
|
249 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
gradio==
|
2 |
-
gTTS==2.4.0
|
3 |
-
pygame==2.5.2
|
4 |
-
openai==0.28.1
|
|
|
1 |
+
gradio==3.50.2
|
2 |
+
gTTS==2.4.0
|
3 |
+
pygame==2.5.2
|
|