Spaces:
Sleeping
Sleeping
Update backend/models.py
Browse files- backend/models.py +29 -4
backend/models.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
|
|
|
|
|
4 |
# تحميل متغيرات البيئة
|
5 |
HUGGING_FACE_API_KEY = os.environ.get("HUGGING_FACE_API_KEY")
|
6 |
|
@@ -18,8 +20,11 @@ def summarizer(text):
|
|
18 |
# ✅ طباعة حالة الاستجابة لمعرفة هل هناك خطأ؟
|
19 |
print("API Response Status Code:", response.status_code)
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
|
24 |
return response.json()
|
25 |
|
@@ -46,7 +51,7 @@ def translator(text, source_lang, target_lang):
|
|
46 |
# تحقق من أن اللغات مدعومة
|
47 |
supported_langs = ['en', 'fr', 'es', 'ar', 'zh']
|
48 |
if source_lang not in supported_langs or target_lang not in supported_langs:
|
49 |
-
return {"error": "
|
50 |
|
51 |
# توليد اسم النموذج بناءً على اللغات
|
52 |
model_name = f"Helsinki-NLP/opus-mt-{source_lang}-{target_lang}"
|
@@ -66,4 +71,24 @@ def translator(text, source_lang, target_lang):
|
|
66 |
|
67 |
# دالة توليد كود التصور البياني
|
68 |
def code_generator(prompt):
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
|
4 |
+
from pdfminer.high_level import extract_text
|
5 |
+
from docx import Document
|
6 |
# تحميل متغيرات البيئة
|
7 |
HUGGING_FACE_API_KEY = os.environ.get("HUGGING_FACE_API_KEY")
|
8 |
|
|
|
20 |
# ✅ طباعة حالة الاستجابة لمعرفة هل هناك خطأ؟
|
21 |
print("API Response Status Code:", response.status_code)
|
22 |
|
23 |
+
try:
|
24 |
+
response_json = response.json() # تحويل الاستجابة إلى JSON
|
25 |
+
print("API Response:", response_json)
|
26 |
+
except Exception as e:
|
27 |
+
print("Error parsing response JSON:", e)
|
28 |
|
29 |
return response.json()
|
30 |
|
|
|
51 |
# تحقق من أن اللغات مدعومة
|
52 |
supported_langs = ['en', 'fr', 'es', 'ar', 'zh']
|
53 |
if source_lang not in supported_langs or target_lang not in supported_langs:
|
54 |
+
return {"error": "there is not support , supported languages: en, fr, es, ar, zh"}
|
55 |
|
56 |
# توليد اسم النموذج بناءً على اللغات
|
57 |
model_name = f"Helsinki-NLP/opus-mt-{source_lang}-{target_lang}"
|
|
|
71 |
|
72 |
# دالة توليد كود التصور البياني
|
73 |
def code_generator(prompt):
|
74 |
+
url = "https://api-inference.huggingface.co/models/Salesforce/codegen-350M-multi"
|
75 |
+
headers = {"Authorization": f"Bearer {HUGGING_FACE_API_KEY}"}
|
76 |
+
response = requests.post(url, headers=headers, json={"inputs": prompt})
|
77 |
+
|
78 |
+
try:
|
79 |
+
return response.json()
|
80 |
+
except Exception as e:
|
81 |
+
return {"error": str(e)}
|
82 |
+
|
83 |
+
|
84 |
+
def extract_text_from_document(path):
|
85 |
+
if path.endswith(".pdf"):
|
86 |
+
return extract_text(path)
|
87 |
+
elif path.endswith(".docx"):
|
88 |
+
doc = Document(path)
|
89 |
+
return "\n".join([p.text for p in doc.paragraphs])
|
90 |
+
elif path.endswith(".txt"):
|
91 |
+
with open(path, encoding="utf-8") as f:
|
92 |
+
return f.read()
|
93 |
+
else:
|
94 |
+
return "Unsupported file format."
|