Spaces:
Runtime error
Runtime error
initial commit
Browse files
app.py
CHANGED
|
@@ -1,63 +1,38 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 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 |
-
|
| 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)
|
|
|