Yihaiyue commited on
Commit
82b60ea
1 Parent(s): 02e2797

Upload customized_final_app.py

Browse files
Files changed (1) hide show
  1. customized_final_app.py +49 -0
customized_final_app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """customized_final_app.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1EsQl5_gVT9N3YJfSlPMm2A2m9kGYTGXn
8
+ """
9
+
10
+ !pip install gradio
11
+
12
+ from transformers import pipeline, set_seed, GenerationConfig, AutoModelForCausalLM
13
+ import gradio as gr
14
+ import torch
15
+
16
+ gpt2_generator = pipeline('text-generation', model='gpt2')
17
+ tinyllama_generator = pipeline('text-generation', model='as-cle-bert/tinyllama-essay-scorer')
18
+
19
+ def load_model(model_name):
20
+ global generator
21
+ generator = pipeline('text-generation', model=model_name, trust_remote_code=True)
22
+
23
+ def generate_text(model, prompt, temperature, max_length, top_p):
24
+ if temperature == 0:
25
+ do_sample = False
26
+ else:
27
+ do_sample = True
28
+
29
+ load_model(model)
30
+
31
+ response = generator(prompt, max_length=max_length, do_sample=do_sample, temperature=temperature, top_p=top_p)[0]["generated_text"]
32
+ return response
33
+
34
+ interface = gr.Interface(
35
+ fn=generate_text,
36
+ inputs=[
37
+ gr.components.Dropdown(label="Choose a Model", choices=['gpt2', 'as-cle-bert/tinyllama-essay-scorer'], value='gpt2', info="Select the model for generating text."),
38
+ gr.components.Dropdown(label="Prompt", choices=['Write a tagline for an ice cream shop', 'Write a poem about spring', 'Write an introduction to the University of Zurich'], value='Write a tagline for an ice cream shop'),
39
+ gr.components.Slider(minimum=0, maximum=2, step=0.01, value = 1, label="Temperature", info = "(For τ = 1, the distribution is unchanged;For τ > 1, the distribution becomes more uniform; For τ < 1, the distribution becomes more peaked.)"),
40
+ gr.components.Slider(minimum=1, maximum=256, step=1, value = 16, label="Max Length", info ="(Maximum length is the maximum limit of the generated text.)"),
41
+ gr.components.Slider(minimum=0, maximum=1, step=0.01, value=1, label="Top-p", info="(Top-p sampling is to keep the top p percent of the probability mass.)")
42
+ ],
43
+ outputs=[gr.Textbox(label="Output", lines=3, placeholder = "Hello, World!")],
44
+ title="Text Generation Control Panel",
45
+ description="Adjust the settings to control the text generation parameters."
46
+ )
47
+
48
+
49
+ interface.launch(share=True)