Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,16 @@
|
|
| 1 |
import subprocess
|
| 2 |
import sys
|
| 3 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 4 |
-
from googletrans import Translator
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
install('transformers')
|
| 12 |
-
install('sentencepiece')
|
| 13 |
-
install('googletrans==4.0.0-rc1')
|
| 14 |
-
install('torch')
|
| 15 |
|
| 16 |
class GOTSummarizer:
|
| 17 |
def __init__(self):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
self.tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 21 |
-
self.model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 22 |
self.translator = Translator()
|
| 23 |
|
| 24 |
def summarize(self, text, max_length=150):
|
|
@@ -38,36 +30,20 @@ class GOTSummarizer:
|
|
| 38 |
print(f"Translation error: {e}")
|
| 39 |
return text
|
| 40 |
|
| 41 |
-
# Initialize summarizer
|
| 42 |
summarizer = GOTSummarizer()
|
| 43 |
|
| 44 |
def process(text, lang='en'):
|
| 45 |
if not text.strip():
|
| 46 |
-
return "Please enter
|
| 47 |
-
|
| 48 |
-
# Summarize the text
|
| 49 |
summary = summarizer.summarize(text)
|
| 50 |
-
|
| 51 |
-
# Translate if needed
|
| 52 |
-
if lang != 'en':
|
| 53 |
-
return summarizer.translate(summary, lang)
|
| 54 |
-
return summary
|
| 55 |
|
| 56 |
-
|
| 57 |
-
interface = gr.Interface(
|
| 58 |
fn=process,
|
| 59 |
inputs=[
|
| 60 |
-
gr.Textbox(label="Input Text", lines=10
|
| 61 |
-
gr.Dropdown(
|
| 62 |
-
label="Language",
|
| 63 |
-
choices=['en', 'hi', 'pa', 'ta', 'bn'],
|
| 64 |
-
value='en'
|
| 65 |
-
)
|
| 66 |
],
|
| 67 |
-
outputs=
|
| 68 |
-
title="Game of Thrones
|
| 69 |
-
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
# Launch the interface
|
| 73 |
-
interface.launch()
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import sys
|
| 3 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Fix httpcore compatibility
|
| 7 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "httpcore==0.15.0", "googletrans==4.0.0-rc1"])
|
| 8 |
+
from googletrans import Translator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class GOTSummarizer:
|
| 11 |
def __init__(self):
|
| 12 |
+
self.tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
| 13 |
+
self.model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
|
|
|
|
|
|
| 14 |
self.translator = Translator()
|
| 15 |
|
| 16 |
def summarize(self, text, max_length=150):
|
|
|
|
| 30 |
print(f"Translation error: {e}")
|
| 31 |
return text
|
| 32 |
|
|
|
|
| 33 |
summarizer = GOTSummarizer()
|
| 34 |
|
| 35 |
def process(text, lang='en'):
|
| 36 |
if not text.strip():
|
| 37 |
+
return "Please enter text to summarize"
|
|
|
|
|
|
|
| 38 |
summary = summarizer.summarize(text)
|
| 39 |
+
return summary if lang == 'en' else summarizer.translate(summary, lang)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
gr.Interface(
|
|
|
|
| 42 |
fn=process,
|
| 43 |
inputs=[
|
| 44 |
+
gr.Textbox(label="Input Text", lines=10),
|
| 45 |
+
gr.Dropdown(['en', 'hi', 'pa', 'ta', 'bn'], label="Language")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
],
|
| 47 |
+
outputs="text",
|
| 48 |
+
title="Game of Thrones Summarizer"
|
| 49 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|