Spaces:
Sleeping
Sleeping
import os | |
import time | |
from dotenv import load_dotenv | |
import requests | |
import gradio as gr | |
import re | |
# Load environment variables | |
load_dotenv() | |
# Access the Hugging Face API key | |
hf_api_key = os.getenv('HF_API_KEY') | |
# Model names and endpoints | |
MODEL_NAME_EN = "sshleifer/distilbart-cnn-12-6" | |
MODEL_NAME_FA = "csebuetnlp/mT5_multilingual_XLSum" | |
MODEL_NAME_FALLBACK = "facebook/bart-large-cnn" # Fallback model that supports multiple languages | |
ENDPOINT_URL_EN = f"https://api-inference.huggingface.co/models/{MODEL_NAME_EN}" | |
ENDPOINT_URL_FA = f"https://api-inference.huggingface.co/models/{MODEL_NAME_FA}" | |
ENDPOINT_URL_FALLBACK = f"https://api-inference.huggingface.co/models/{MODEL_NAME_FALLBACK}" | |
def is_persian(text): | |
persian_pattern = re.compile(r'[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF]+') | |
return bool(persian_pattern.search(text)) | |
def get_completion(inputs, is_persian=False, max_retries=3): | |
headers = { | |
"Authorization": f"Bearer {hf_api_key}", | |
"Content-Type": "application/json" | |
} | |
data = { | |
"inputs": inputs, | |
"parameters": {"max_length": 150, "min_length": 30} | |
} | |
endpoint_url = ENDPOINT_URL_FA if is_persian else ENDPOINT_URL_EN | |
for attempt in range(max_retries): | |
try: | |
response = requests.post(endpoint_url, headers=headers, json=data, timeout=10) | |
response.raise_for_status() | |
return response.json() | |
except requests.exceptions.RequestException as e: | |
print(f"Attempt {attempt + 1} failed: {e}") | |
if attempt == max_retries - 1: | |
print("All attempts failed. Trying fallback model.") | |
try: | |
response = requests.post(ENDPOINT_URL_FALLBACK, headers=headers, json=data, timeout=10) | |
response.raise_for_status() | |
return response.json() | |
except requests.exceptions.RequestException as fallback_error: | |
print(f"Fallback model failed: {fallback_error}") | |
return {"error": f"All attempts failed: {str(fallback_error)}"} | |
time.sleep(2 ** attempt) # Exponential backoff | |
def summarize(input_text): | |
try: | |
is_persian_text = is_persian(input_text) | |
output = get_completion(input_text, is_persian_text) | |
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]: | |
return output[0]['summary_text'] | |
elif isinstance(output, dict): | |
if 'summary_text' in output: | |
return output['summary_text'] | |
elif 'error' in output: | |
return f"Error: {output['error']}" | |
else: | |
return f"Unexpected response format: {output}" | |
except Exception as e: | |
return f"An error occurred: {str(e)}" | |
# Example texts (unchanged) | |
english_example = """ | |
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. | |
""" | |
persian_example = """ | |
اینترنت اشیا (IoT) در حال تغییر شیوه زندگی و کار ما است. این مفهوم به شبکهای از دستگاههای فیزیکی، وسایل نقلیه، لوازم خانگی و سایر اقلامی اشاره دارد که با الکترونیک، نرمافزار، سنسورها و اتصال به شبکه تعبیه شدهاند و این امکان را فراهم میکنند تا این اشیاء دادهها را جمعآوری و تبادل کنند. از خانههای هوشمندی که به طور خودکار دما و روشنایی را تنظیم میکنند تا سنسورهای صنعتی که خرابی تجهیزات را پیشبینی میکنند، IoT در حال ایجاد محیطهایی کارآمدتر، پاسخگوتر و مبتنی بر داده است. با این حال، با افزایش شیوع دستگاههای IoT، نگرانیهای مربوط به حریم خصوصی و امنیت دادهها نیز در حال افزایش است که نیاز به اقدامات و مقررات قوی امنیت سایبری را ضروری میسازد. | |
""" | |
# Create and launch the Gradio interface | |
demo = gr.Interface( | |
fn=summarize, | |
inputs=gr.Textbox(lines=8, label="Enter text to summarize (English or Persian)"), | |
outputs=gr.Textbox(label="Summary"), | |
title="Multilingual Text Summarization", | |
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.", | |
examples=[ | |
[english_example], | |
[persian_example] | |
] | |
) | |
demo.launch() |