Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ from text_generation import Client
|
|
8 |
|
9 |
from share_btn import community_icon_html, loading_icon_html, share_js, share_btn_css
|
10 |
|
11 |
-
HF_TOKEN = os.environ.get("
|
12 |
API_URL = os.environ.get("API_URL")
|
13 |
|
14 |
|
@@ -19,34 +19,14 @@ theme = gr.themes.Monochrome(
|
|
19 |
radius_size=gr.themes.sizes.radius_sm,
|
20 |
font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
|
21 |
)
|
22 |
-
if HF_TOKEN:
|
23 |
-
try:
|
24 |
-
shutil.rmtree("./data/")
|
25 |
-
except:
|
26 |
-
pass
|
27 |
-
|
28 |
-
repo = Repository(
|
29 |
-
local_dir="./data/", clone_from="trl-lib/stack-llama-prompts", use_auth_token=HF_TOKEN, repo_type="dataset"
|
30 |
-
)
|
31 |
-
repo.git_pull()
|
32 |
|
33 |
client = Client(
|
34 |
API_URL,
|
35 |
-
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
36 |
)
|
37 |
|
38 |
-
PROMPT_TEMPLATE = """Question: {prompt}\n\nAnswer:"""
|
39 |
-
|
40 |
-
|
41 |
-
def save_inputs_and_outputs(inputs, outputs, generate_kwargs):
|
42 |
-
with open(os.path.join("data", "prompts.jsonl"), "a") as f:
|
43 |
-
json.dump({"inputs": inputs, "outputs": outputs, "generate_kwargs": generate_kwargs}, f, ensure_ascii=False)
|
44 |
-
f.write("\n")
|
45 |
-
commit_url = repo.push_to_hub()
|
46 |
-
|
47 |
|
48 |
-
def generate(
|
49 |
-
formatted_instruction = PROMPT_TEMPLATE.format(prompt=instruction)
|
50 |
|
51 |
temperature = float(temperature)
|
52 |
if temperature < 1e-2:
|
@@ -65,7 +45,7 @@ def generate(instruction, temperature=0.9, max_new_tokens=256, top_p=0.95, repet
|
|
65 |
)
|
66 |
|
67 |
stream = client.generate_stream(
|
68 |
-
|
69 |
**generate_kwargs,
|
70 |
)
|
71 |
|
@@ -73,23 +53,11 @@ def generate(instruction, temperature=0.9, max_new_tokens=256, top_p=0.95, repet
|
|
73 |
for response in stream:
|
74 |
output += response.token.text
|
75 |
yield output
|
76 |
-
if HF_TOKEN and do_save:
|
77 |
-
try:
|
78 |
-
print("Pushing prompt and completion to the Hub")
|
79 |
-
save_inputs_and_outputs(formatted_instruction, output, generate_kwargs)
|
80 |
-
except Exception as e:
|
81 |
-
print(e)
|
82 |
-
|
83 |
return output
|
84 |
|
85 |
|
86 |
examples = [
|
87 |
-
"
|
88 |
-
"What are the various algorithms to sort a list?",
|
89 |
-
"How can I sort a list in Python?",
|
90 |
-
"How do I ask a question in StackOverflow?",
|
91 |
-
"How to beat a Hitmonlee in a Pokemon battle?",
|
92 |
-
"How can I write a Java function to generate the nth Fibonacci number?",
|
93 |
]
|
94 |
|
95 |
|
@@ -103,35 +71,17 @@ css = ".generating {visibility: hidden}" + share_btn_css
|
|
103 |
with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
|
104 |
with gr.Column():
|
105 |
gr.Markdown(
|
106 |
-
"""
|
107 |
-
|
108 |
-
|
109 |
-
StackLLaMa is a 7 billion parameter language model based on [Meta's LLaMA model](https://ai.facebook.com/blog/large-language-model-llama-meta-ai/) that has been trained on pairs of questions and answers from [Stack Exchange](https://stackexchange.com) using Reinforcement Learning from Human Feedback (RLHF) with the [TRL library](https://github.com/lvwerra/trl). For more details, check out our [blog post](https://huggingface.co/blog/stackllama).
|
110 |
-
|
111 |
-
Type in the box below and click the button to generate answers to your most pressing questions!
|
112 |
-
|
113 |
-
⚠️ **Intended Use**: this app and its [supporting model](https://huggingface.co/trl-lib/llama-7b-se-rl-peft) are provided as educational tools to explain RLHF with the TRL library; not to serve as replacement for human expertise. For more details on the model's limitations in terms of factuality and biases, see the [model card.](https://huggingface.co/trl-lib/llama-7b-se-rl-peft#intended-uses--limitations)
|
114 |
-
|
115 |
-
⚠️ **Data Collection**: by default, we are collecting the prompts entered in this app to further improve and evaluate the model. Do not share any personal or sensitive information while using the app! You can opt out of this data collection by removing the checkbox below:
|
116 |
-
"""
|
117 |
)
|
118 |
with gr.Row():
|
119 |
with gr.Column(scale=3):
|
120 |
-
|
121 |
-
value=True,
|
122 |
-
label="Store data",
|
123 |
-
info="You agree to the storage of your prompt and generated text for research and development purposes:")
|
124 |
-
instruction = gr.Textbox(placeholder="Enter your question here", label="Question", elem_id="q-input")
|
125 |
|
126 |
-
|
127 |
with gr.Box():
|
128 |
-
gr.Markdown("**
|
129 |
output = gr.Markdown(elem_id="q-output")
|
130 |
submit = gr.Button("Generate", variant="primary")
|
131 |
-
with gr.Group(elem_id="share-btn-container"):
|
132 |
-
community_icon = gr.HTML(community_icon_html, visible=True)
|
133 |
-
loading_icon = gr.HTML(loading_icon_html, visible=True)
|
134 |
-
share_button = gr.Button("Share to community", elem_id="share-btn", visible=True)
|
135 |
gr.Examples(
|
136 |
examples=examples,
|
137 |
inputs=[instruction],
|
@@ -181,6 +131,5 @@ with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
|
|
181 |
|
182 |
submit.click(generate, inputs=[instruction, temperature, max_new_tokens, top_p, repetition_penalty, do_save], outputs=[output])
|
183 |
instruction.submit(generate, inputs=[instruction, temperature, max_new_tokens, top_p, repetition_penalty], outputs=[output])
|
184 |
-
share_button.click(None, [], [], _js=share_js)
|
185 |
|
186 |
demo.queue(concurrency_count=16).launch(debug=True)
|
|
|
8 |
|
9 |
from share_btn import community_icon_html, loading_icon_html, share_js, share_btn_css
|
10 |
|
11 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
12 |
API_URL = os.environ.get("API_URL")
|
13 |
|
14 |
|
|
|
19 |
radius_size=gr.themes.sizes.radius_sm,
|
20 |
font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
|
21 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
client = Client(
|
24 |
API_URL,
|
25 |
+
#headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
26 |
)
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
def generate(prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0, do_save=True):
|
|
|
30 |
|
31 |
temperature = float(temperature)
|
32 |
if temperature < 1e-2:
|
|
|
45 |
)
|
46 |
|
47 |
stream = client.generate_stream(
|
48 |
+
prompt,
|
49 |
**generate_kwargs,
|
50 |
)
|
51 |
|
|
|
53 |
for response in stream:
|
54 |
output += response.token.text
|
55 |
yield output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
return output
|
57 |
|
58 |
|
59 |
examples = [
|
60 |
+
"def hello_world():",
|
|
|
|
|
|
|
|
|
|
|
61 |
]
|
62 |
|
63 |
|
|
|
71 |
with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo:
|
72 |
with gr.Column():
|
73 |
gr.Markdown(
|
74 |
+
""" # BigCode - Playground
|
75 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
)
|
77 |
with gr.Row():
|
78 |
with gr.Column(scale=3):
|
79 |
+
instruction = gr.Textbox(placeholder="Enter your prompt here", label="Prompt", elem_id="q-input")
|
|
|
|
|
|
|
|
|
80 |
|
|
|
81 |
with gr.Box():
|
82 |
+
gr.Markdown("**Response**")
|
83 |
output = gr.Markdown(elem_id="q-output")
|
84 |
submit = gr.Button("Generate", variant="primary")
|
|
|
|
|
|
|
|
|
85 |
gr.Examples(
|
86 |
examples=examples,
|
87 |
inputs=[instruction],
|
|
|
131 |
|
132 |
submit.click(generate, inputs=[instruction, temperature, max_new_tokens, top_p, repetition_penalty, do_save], outputs=[output])
|
133 |
instruction.submit(generate, inputs=[instruction, temperature, max_new_tokens, top_p, repetition_penalty], outputs=[output])
|
|
|
134 |
|
135 |
demo.queue(concurrency_count=16).launch(debug=True)
|