Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,40 @@
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
import requests
|
4 |
-
import json
|
5 |
import gradio as gr
|
|
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
9 |
|
10 |
-
# Access the Hugging Face API key
|
11 |
hf_api_key = os.getenv('HF_API_KEY')
|
12 |
-
MODEL_NAME = "sshleifer/distilbart-cnn-12-6"
|
13 |
-
ENDPOINT_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
headers = {
|
17 |
"Authorization": f"Bearer {hf_api_key}",
|
18 |
"Content-Type": "application/json"
|
19 |
}
|
20 |
data = {
|
21 |
-
"inputs": inputs
|
|
|
22 |
}
|
23 |
-
|
24 |
-
|
25 |
|
26 |
try:
|
27 |
-
response = requests.post(
|
28 |
response.raise_for_status()
|
29 |
return response.json()
|
30 |
except requests.exceptions.RequestException as e:
|
@@ -33,13 +43,38 @@ def get_completion(inputs, parameters=None):
|
|
33 |
|
34 |
def summarize(input_text):
|
35 |
try:
|
36 |
-
|
|
|
|
|
37 |
if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
|
38 |
return output[0]['summary_text']
|
|
|
|
|
39 |
else:
|
40 |
return f"Unexpected response format: {output}"
|
41 |
except Exception as e:
|
42 |
return f"An error occurred: {str(e)}"
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
demo.launch()
|
|
|
1 |
import os
|
2 |
from dotenv import load_dotenv
|
3 |
import requests
|
|
|
4 |
import gradio as gr
|
5 |
+
import re
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
9 |
|
10 |
+
# Access the Hugging Face API key
|
11 |
hf_api_key = os.getenv('HF_API_KEY')
|
|
|
|
|
12 |
|
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"
|
28 |
}
|
29 |
data = {
|
30 |
+
"inputs": inputs,
|
31 |
+
"parameters": {"max_length": 150, "min_length": 30}
|
32 |
}
|
33 |
+
|
34 |
+
endpoint_url = ENDPOINT_URL_FA if is_persian else ENDPOINT_URL_EN
|
35 |
|
36 |
try:
|
37 |
+
response = requests.post(endpoint_url, headers=headers, json=data)
|
38 |
response.raise_for_status()
|
39 |
return response.json()
|
40 |
except requests.exceptions.RequestException as e:
|
|
|
43 |
|
44 |
def summarize(input_text):
|
45 |
try:
|
46 |
+
is_persian_text = is_persian(input_text)
|
47 |
+
output = get_completion(input_text, is_persian_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) and 'summary_text' in output:
|
52 |
+
return output['summary_text']
|
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 |
+
"""
|
62 |
+
|
63 |
+
persian_example = """
|
64 |
+
اینترنت اشیا (IoT) در حال تغییر شیوه زندگی و کار ما است. این مفهوم به شبکهای از دستگاههای فیزیکی، وسایل نقلیه، لوازم خانگی و سایر اقلامی اشاره دارد که با الکترونیک، نرمافزار، سنسورها و اتصال به شبکه تعبیه شدهاند و این امکان را فراهم میکنند تا این اشیاء دادهها را جمعآوری و تبادل کنند. از خانههای هوشمندی که به طور خودکار دما و روشنایی را تنظیم میکنند تا سنسورهای صنعتی که خرابی تجهیزات را پیشبینی میکنند، IoT در حال ایجاد محیطهایی کارآمدتر، پاسخگوتر و مبتنی بر داده است. با این حال، با افزایش شیوع دستگاههای IoT، نگرانیهای مربوط به حریم خصوصی و امنیت دادهها نیز در حال افزایش است که نیاز به اقدامات و مقررات قوی امنیت سایبری را ضروری میسازد.
|
65 |
+
"""
|
66 |
+
|
67 |
+
# Create and launch the Gradio interface
|
68 |
+
demo = gr.Interface(
|
69 |
+
fn=summarize,
|
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]
|
77 |
+
]
|
78 |
+
)
|
79 |
+
|
80 |
demo.launch()
|