QinghaoGuan commited on
Commit
6805fa0
1 Parent(s): 30ac935

initial commit

Browse files
Files changed (1) hide show
  1. app.py +33 -58
app.py CHANGED
@@ -1,63 +1,38 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
  ],
 
 
 
59
  )
60
 
61
 
62
- if __name__ == "__main__":
63
- demo.launch()
 
1
+ from transformers import pipeline, set_seed, GenerationConfig, AutoModelForCausalLM
2
  import gradio as gr
3
+ import torch
4
+
5
+ gpt2_generator = pipeline('text-generation', model='gpt2')
6
+ tinyllama_generator = pipeline('text-generation', model='as-cle-bert/tinyllama-essay-scorer')
7
+
8
+ def load_model(model_name):
9
+ global generator
10
+ generator = pipeline('text-generation', model=model_name, trust_remote_code=True)
11
+
12
+ def generate_text(model, prompt, temperature, max_length, top_p):
13
+ if temperature == 0:
14
+ do_sample = False
15
+ else:
16
+ do_sample = True
17
+
18
+ load_model(model)
19
+
20
+ response = generator(prompt, max_length=max_length, do_sample=do_sample, temperature=temperature, top_p=top_p)[0]["generated_text"]
21
+ return response
22
+
23
+ interface = gr.Interface(
24
+ fn=generate_text,
25
+ inputs=[
26
+ gr.components.Dropdown(label="Choose a Model", choices=['gpt2', 'as-cle-bert/tinyllama-essay-scorer'], value='gpt2', info="Select the model for generating text."),
27
+ 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'),
28
+ 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.)"),
29
+ 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.)"),
30
+ 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.)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  ],
32
+ outputs=[gr.Textbox(label="Output", lines=3, placeholder = "Hello, World!")],
33
+ title="Text Generation Control Panel",
34
+ description="Adjust the settings to control the text generation parameters."
35
  )
36
 
37
 
38
+ interface.launch(share=True)