Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
from dotenv import load_dotenv
|
3 |
import requests
|
4 |
import gradio as gr
|
@@ -13,15 +14,16 @@ hf_api_key = os.getenv('HF_API_KEY')
|
|
13 |
# Model names and endpoints
|
14 |
MODEL_NAME_EN = "sshleifer/distilbart-cnn-12-6"
|
15 |
MODEL_NAME_FA = "csebuetnlp/mT5_multilingual_XLSum"
|
|
|
16 |
ENDPOINT_URL_EN = f"https://api-inference.huggingface.co/models/{MODEL_NAME_EN}"
|
17 |
ENDPOINT_URL_FA = f"https://api-inference.huggingface.co/models/{MODEL_NAME_FA}"
|
|
|
18 |
|
19 |
def is_persian(text):
|
20 |
-
# Simple check for Persian characters
|
21 |
persian_pattern = re.compile(r'[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF]+')
|
22 |
return bool(persian_pattern.search(text))
|
23 |
|
24 |
-
def get_completion(inputs, is_persian=False):
|
25 |
headers = {
|
26 |
"Authorization": f"Bearer {hf_api_key}",
|
27 |
"Content-Type": "application/json"
|
@@ -33,13 +35,23 @@ def get_completion(inputs, is_persian=False):
|
|
33 |
|
34 |
endpoint_url = ENDPOINT_URL_FA if is_persian else ENDPOINT_URL_EN
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
def summarize(input_text):
|
45 |
try:
|
@@ -48,14 +60,17 @@ def summarize(input_text):
|
|
48 |
|
49 |
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
|
50 |
return output[0]['summary_text']
|
51 |
-
elif isinstance(output, dict)
|
52 |
-
|
|
|
|
|
|
|
53 |
else:
|
54 |
return f"Unexpected response format: {output}"
|
55 |
except Exception as e:
|
56 |
return f"An error occurred: {str(e)}"
|
57 |
|
58 |
-
# Example texts
|
59 |
english_example = """
|
60 |
The Internet of Things (IoT) is transforming the way we live and work. It refers to the interconnected network of physical devices, vehicles, home appliances, and other items embedded with electronics, software, sensors, and network connectivity, which enables these objects to collect and exchange data. From smart homes that adjust temperature and lighting automatically to industrial sensors that predict equipment failures, IoT is creating more efficient, responsive, and data-driven environments. However, as IoT devices become more prevalent, concerns about data privacy and security are also growing, necessitating robust cybersecurity measures and regulations.
|
61 |
"""
|
@@ -70,7 +85,7 @@ demo = gr.Interface(
|
|
70 |
inputs=gr.Textbox(lines=8, label="Enter text to summarize (English or Persian)"),
|
71 |
outputs=gr.Textbox(label="Summary"),
|
72 |
title="Multilingual Text Summarization",
|
73 |
-
description="This app summarizes text in English or Persian. It automatically detects the language and uses the appropriate model.",
|
74 |
examples=[
|
75 |
[english_example],
|
76 |
[persian_example]
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
from dotenv import load_dotenv
|
4 |
import requests
|
5 |
import gradio as gr
|
|
|
14 |
# Model names and endpoints
|
15 |
MODEL_NAME_EN = "sshleifer/distilbart-cnn-12-6"
|
16 |
MODEL_NAME_FA = "csebuetnlp/mT5_multilingual_XLSum"
|
17 |
+
MODEL_NAME_FALLBACK = "facebook/bart-large-cnn" # Fallback model that supports multiple languages
|
18 |
ENDPOINT_URL_EN = f"https://api-inference.huggingface.co/models/{MODEL_NAME_EN}"
|
19 |
ENDPOINT_URL_FA = f"https://api-inference.huggingface.co/models/{MODEL_NAME_FA}"
|
20 |
+
ENDPOINT_URL_FALLBACK = f"https://api-inference.huggingface.co/models/{MODEL_NAME_FALLBACK}"
|
21 |
|
22 |
def is_persian(text):
|
|
|
23 |
persian_pattern = re.compile(r'[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF]+')
|
24 |
return bool(persian_pattern.search(text))
|
25 |
|
26 |
+
def get_completion(inputs, is_persian=False, max_retries=3):
|
27 |
headers = {
|
28 |
"Authorization": f"Bearer {hf_api_key}",
|
29 |
"Content-Type": "application/json"
|
|
|
35 |
|
36 |
endpoint_url = ENDPOINT_URL_FA if is_persian else ENDPOINT_URL_EN
|
37 |
|
38 |
+
for attempt in range(max_retries):
|
39 |
+
try:
|
40 |
+
response = requests.post(endpoint_url, headers=headers, json=data, timeout=10)
|
41 |
+
response.raise_for_status()
|
42 |
+
return response.json()
|
43 |
+
except requests.exceptions.RequestException as e:
|
44 |
+
print(f"Attempt {attempt + 1} failed: {e}")
|
45 |
+
if attempt == max_retries - 1:
|
46 |
+
print("All attempts failed. Trying fallback model.")
|
47 |
+
try:
|
48 |
+
response = requests.post(ENDPOINT_URL_FALLBACK, headers=headers, json=data, timeout=10)
|
49 |
+
response.raise_for_status()
|
50 |
+
return response.json()
|
51 |
+
except requests.exceptions.RequestException as fallback_error:
|
52 |
+
print(f"Fallback model failed: {fallback_error}")
|
53 |
+
return {"error": f"All attempts failed: {str(fallback_error)}"}
|
54 |
+
time.sleep(2 ** attempt) # Exponential backoff
|
55 |
|
56 |
def summarize(input_text):
|
57 |
try:
|
|
|
60 |
|
61 |
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
|
62 |
return output[0]['summary_text']
|
63 |
+
elif isinstance(output, dict):
|
64 |
+
if 'summary_text' in output:
|
65 |
+
return output['summary_text']
|
66 |
+
elif 'error' in output:
|
67 |
+
return f"Error: {output['error']}"
|
68 |
else:
|
69 |
return f"Unexpected response format: {output}"
|
70 |
except Exception as e:
|
71 |
return f"An error occurred: {str(e)}"
|
72 |
|
73 |
+
# Example texts (unchanged)
|
74 |
english_example = """
|
75 |
The Internet of Things (IoT) is transforming the way we live and work. It refers to the interconnected network of physical devices, vehicles, home appliances, and other items embedded with electronics, software, sensors, and network connectivity, which enables these objects to collect and exchange data. From smart homes that adjust temperature and lighting automatically to industrial sensors that predict equipment failures, IoT is creating more efficient, responsive, and data-driven environments. However, as IoT devices become more prevalent, concerns about data privacy and security are also growing, necessitating robust cybersecurity measures and regulations.
|
76 |
"""
|
|
|
85 |
inputs=gr.Textbox(lines=8, label="Enter text to summarize (English or Persian)"),
|
86 |
outputs=gr.Textbox(label="Summary"),
|
87 |
title="Multilingual Text Summarization",
|
88 |
+
description="This app summarizes text in English or Persian. It automatically detects the language and uses the appropriate model. If the primary model is unavailable, it will use a fallback model.",
|
89 |
examples=[
|
90 |
[english_example],
|
91 |
[persian_example]
|