ImHadis's picture
Update app.py
a7541af verified
raw
history blame
4.29 kB
import os
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"
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}"
def is_persian(text):
# Simple check for Persian characters
persian_pattern = re.compile(r'[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF]+')
return bool(persian_pattern.search(text))
def get_completion(inputs, is_persian=False):
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
try:
response = requests.post(endpoint_url, headers=headers, json=data)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return {"error": f"Request failed: {str(e)}"}
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) and 'summary_text' in output:
return output['summary_text']
else:
return f"Unexpected response format: {output}"
except Exception as e:
return f"An error occurred: {str(e)}"
# Example texts
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.",
examples=[
[english_example],
[persian_example]
]
)
demo.launch()