Adriiiii24 commited on
Commit
f00ce12
·
verified ·
1 Parent(s): 49b03d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -4
app.py CHANGED
@@ -1,20 +1,41 @@
1
  import gradio as gr
2
- from transformers import pipeline, T5Tokenizer, T5ForConditionalGeneration
3
  import os
 
 
 
4
  token = os.getenv("HF_TOKEN")
 
5
 
 
6
  tokenizer = T5Tokenizer.from_pretrained("sumedh/t5-base-amazonreviews", clean_up_tokenization_spaces=True)
7
  model = T5ForConditionalGeneration.from_pretrained("sumedh/t5-base-amazonreviews")
8
- summarizer = pipeline("summarization", model="sumedh/t5-base-amazonreviews")
 
 
 
9
 
 
10
  def texto_sum(text):
11
- summary = summarizer(text, do_sample=False)
12
- return summary[0]['summary_text']
 
 
 
 
 
 
 
 
 
 
13
 
 
14
  demo = gr.Interface(
15
  fn=texto_sum,
16
  inputs=gr.Textbox(label="Texto a introducir:", placeholder="Introduce el texto a resumir aquí..."),
17
  outputs="text"
18
  )
19
 
 
20
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline, T5Tokenizer, T5ForConditionalGeneration, AutoTokenizer, AutoModelForSeq2SeqLM
3
  import os
4
+ import requests
5
+
6
+ # Load environment variable for Hugging Face API token
7
  token = os.getenv("HF_TOKEN")
8
+ headers = {"Authorization": f"Bearer {token}"}
9
 
10
+ # Load summarization model and tokenizer
11
  tokenizer = T5Tokenizer.from_pretrained("sumedh/t5-base-amazonreviews", clean_up_tokenization_spaces=True)
12
  model = T5ForConditionalGeneration.from_pretrained("sumedh/t5-base-amazonreviews")
13
+ summarizer = pipeline("summarization", model=model, tokenizer=tokenizer)
14
+
15
+ # Translation API details
16
+ API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es"
17
 
18
+ # Summarization and Translation Function
19
  def texto_sum(text):
20
+ # Summarize the input text
21
+ summary = summarizer(text, do_sample=False)[0]['summary_text']
22
+
23
+ # Translate summary using the Hugging Face API
24
+ response = requests.post(API_URL, headers=headers, json={"inputs": summary})
25
+ translation = response.json()
26
+
27
+ # Check if translation is successful
28
+ if 'error' in translation:
29
+ return f"Error in translation: {translation['error']}"
30
+
31
+ return translation[0]['translation_text']
32
 
33
+ # Gradio interface
34
  demo = gr.Interface(
35
  fn=texto_sum,
36
  inputs=gr.Textbox(label="Texto a introducir:", placeholder="Introduce el texto a resumir aquí..."),
37
  outputs="text"
38
  )
39
 
40
+ # Launch the interface
41
  demo.launch()