Spaces:
Sleeping
Sleeping
Commit
·
a9c5704
1
Parent(s):
2315483
Initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""app.py
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1ORnyeMQYmIQwXKecOr52Fr5YOzjrsxvn
|
8 |
+
"""
|
9 |
+
|
10 |
+
# Commented out IPython magic to ensure Python compatibility.
|
11 |
+
# %%capture
|
12 |
+
# !pip install gradio transformers==4.28.0 datasets
|
13 |
+
|
14 |
+
import gradio as gr
|
15 |
+
|
16 |
+
def greet(name):
|
17 |
+
return "Hello " + name
|
18 |
+
|
19 |
+
|
20 |
+
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
21 |
+
|
22 |
+
demo.launch()
|
23 |
+
|
24 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
25 |
+
from datasets import load_dataset
|
26 |
+
import numpy as np
|
27 |
+
|
28 |
+
tokenizer = AutoTokenizer.from_pretrained("mehnaazasad/bart-large-finetuned-arxiv-co-ga-latest")
|
29 |
+
|
30 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("mehnaazasad/bart-large-finetuned-arxiv-co-ga-latest")
|
31 |
+
|
32 |
+
dataset = load_dataset("mehnaazasad/arxiv_astro_co_ga")
|
33 |
+
|
34 |
+
def summarize(text, temperature):
|
35 |
+
num_beams = 5
|
36 |
+
temp = temperature
|
37 |
+
top_k = 35
|
38 |
+
top_p = 0.94
|
39 |
+
inputs = tokenizer(text, return_tensors="pt").input_ids
|
40 |
+
output = model.generate(inputs, max_length=50,
|
41 |
+
num_beams=num_beams, temperature=temp,
|
42 |
+
top_k=top_k, top_p=top_p,
|
43 |
+
do_sample=True)
|
44 |
+
title = tokenizer.decode(output[0], skip_special_tokens=True)
|
45 |
+
return title
|
46 |
+
|
47 |
+
sample = dataset['test']['abstract'][0]
|
48 |
+
|
49 |
+
summarize(sample, 3.0)
|
50 |
+
|
51 |
+
title = "Title Generator"
|
52 |
+
description = """This model was trained to summarize text and generate a title.
|
53 |
+
You can find more details about the fine-tuning of this BART model
|
54 |
+
[here](https://huggingface.co/mehnaazasad/bart-large-finetuned-arxiv-co-ga-latest).
|
55 |
+
While default parameter values are shown, feel free to experiment!
|
56 |
+
<img src="https://adapterhub.ml/static/images/BARTLogo.png" width=200px>
|
57 |
+
"""
|
58 |
+
|
59 |
+
article="[Image credit](https://adapterhub.ml/blog/2021/04/adapters-for-generative-and-seq2seq-models-in-nlp/)"
|
60 |
+
|
61 |
+
gr.Interface(
|
62 |
+
summarize,
|
63 |
+
[
|
64 |
+
gr.Textbox(type="text", label="Paste text here"),
|
65 |
+
gr.Slider(minimum=0.4, maximum=2.0, step=0.2, value=0.7,
|
66 |
+
label="Temperature: crank this up for more creativity (travel beyond 1 at your own risk!)"),
|
67 |
+
],
|
68 |
+
gr.Textbox(type="text", label="Your title is"),
|
69 |
+
title=title,
|
70 |
+
description=description,
|
71 |
+
article=article,
|
72 |
+
theme="finlaymacklon/boxy_violet",
|
73 |
+
).launch()
|