Norod78 commited on
Commit
72a11a5
โ€ข
1 Parent(s): 71821ba
Files changed (2) hide show
  1. app.py +59 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import spaces
5
+
6
+ title = "Fakypedia"
7
+
8
+ DESCRIPTION = """\
9
+ # Genarate a silly article
10
+ A bilingual (English and Hebrew) [nonsense generation model](https://huggingface.co/Norod78/SmolLM-135M-FakyPedia-EngHeb) which produces silly Wikipedia-like abstract text.
11
+ Tap on the \"Submit\" button to generate a silly and/or fake \"Wikipedia-Like\" article based on the input title
12
+ """
13
+
14
+ article = "<p>This model extended the tokenizer of and is a fine-tuned of [SmolLM-135M-Instruct](https://huggingface.co/HuggingFaceTB/SmolLM-135M-Instruct)</p>"
15
+
16
+ CUDA_AVAILABLE = torch.cuda.is_available()
17
+ device = torch.device("cuda" if CUDA_AVAILABLE else "cpu")
18
+
19
+ model_id = "Norod78/SmolLM-135M-FakyPedia-EngHeb"
20
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
21
+ tokenizer.pad_token_id = tokenizer.eos_token_id
22
+ bos_token = tokenizer.bos_token
23
+ model = AutoModelForCausalLM.from_pretrained(model_id).to(device)
24
+ model.generation_config.pad_token_id = tokenizer.pad_token_id
25
+
26
+ torch.manual_seed(1234)
27
+
28
+ @spaces.GPU
29
+ def generate_fakypedia(article_title: str):
30
+ with torch.no_grad():
31
+ result = ""
32
+ string_to_tokenize= f"{bos_token}\\%{article_title}"
33
+ input_ids = tokenizer( string_to_tokenize, return_tensors="pt").input_ids.to(device)
34
+ sample_outputs = model.generate(input_ids, do_sample=True,repetition_penalty=1.2, temperature=0.5, max_length=96, num_return_sequences=3)
35
+ if article_title == None or len(article_title) == 0:
36
+ result += f"# Fakypedia results with random titles \n"
37
+ article_title = ""
38
+ else:
39
+ result += f"# Fakypedia results for \"{article_title}\" \n"
40
+ for i, sample_output in enumerate(sample_outputs):
41
+ decoded_output = tokenizer.decode(sample_output, skip_special_tokens=True)
42
+ decoded_output = decoded_output.replace(f"\%{article_title}", f"## {article_title}").replace("\%", " ").replace("\\n", " \n")
43
+ decoded_output = decoded_output.replace("## \n", "\n")
44
+ result += "{}\n".format(decoded_output)
45
+ return result
46
+
47
+ demo = gr.Interface(
48
+ generate_fakypedia,
49
+ inputs=gr.Textbox(lines=1, label="Enter a title for the article (or leave blank for a random one)"),
50
+ outputs=gr.Markdown(label="Generated fakypedia article"),
51
+ title=title,
52
+ description=DESCRIPTION,
53
+ article=article,
54
+ examples=["Hugging face", "A socially awkward potato", "ื“ื•ืจื•ืŸ ืื“ืœืจ", ""],
55
+ allow_flagging="never",
56
+ )
57
+
58
+ demo.queue()
59
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ accelerate
3
+ torch
4
+ transformers
5
+ tokenizers
6
+ spaces
7
+ numpy