Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,61 @@
|
|
| 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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
# Add a few-shot context to guide the chatbot
|
| 21 |
-
rc_qa_examples = [
|
| 22 |
-
("What is Resilient Coders?", "Resilient Coders is a nonprofit that trains young people of color for careers in tech."),
|
| 23 |
-
("Is the bootcamp free?", "Yes, the bootcamp is completely free and includes a stipend."),
|
| 24 |
-
("How long is the program?", "It usually runs for about 14 to 20 weeks."),
|
| 25 |
-
("Do I need to know how to code?", "No prior experience is required. We train participants from the ground up."),
|
| 26 |
-
("Is it remote or in-person?", "The program may be remote, in-person, or hybrid depending on the cohort."),
|
| 27 |
-
]
|
| 28 |
for q, a in rc_qa_examples:
|
| 29 |
messages.append({"role": "user", "content": q})
|
| 30 |
messages.append({"role": "assistant", "content": a})
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# Append current user message
|
| 40 |
messages.append({"role": "user", "content": message})
|
| 41 |
|
| 42 |
# Stream response
|
| 43 |
response = ""
|
| 44 |
-
for
|
| 45 |
-
messages,
|
| 46 |
max_tokens=max_tokens,
|
| 47 |
-
stream=True,
|
| 48 |
temperature=temperature,
|
| 49 |
top_p=top_p,
|
|
|
|
| 50 |
):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
|
|
|
| 56 |
demo = gr.ChatInterface(
|
| 57 |
-
respond,
|
| 58 |
additional_inputs=[
|
| 59 |
-
gr.Textbox(value="You are a helpful assistant
|
| 60 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max
|
| 61 |
-
gr.Slider(minimum=0.1, maximum=
|
| 62 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p
|
| 63 |
],
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
-
|
| 67 |
if __name__ == "__main__":
|
| 68 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Connect to Hugging Face model
|
|
|
|
|
|
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# Example Q&A pairs about Resilient Coders
|
| 8 |
+
rc_qa_examples = [
|
| 9 |
+
("What is Resilient Coders?", "Resilient Coders is a nonprofit that trains young people of color for careers in tech."),
|
| 10 |
+
("Is the bootcamp free?", "Yes, the bootcamp is completely free and includes a stipend."),
|
| 11 |
+
("How long is the program?", "It usually runs for about 14 to 20 weeks."),
|
| 12 |
+
("Do I need to know how to code?", "No prior experience is required. We train participants from the ground up."),
|
| 13 |
+
("Is it remote or in-person?", "The program may be remote, in-person, or hybrid depending on the cohort."),
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
# Main response function
|
| 17 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 18 |
+
# Format prompt
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
| 20 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
for q, a in rc_qa_examples:
|
| 22 |
messages.append({"role": "user", "content": q})
|
| 23 |
messages.append({"role": "assistant", "content": a})
|
| 24 |
+
|
| 25 |
+
for user_msg, bot_reply in history:
|
| 26 |
+
if user_msg:
|
| 27 |
+
messages.append({"role": "user", "content": user_msg})
|
| 28 |
+
if bot_reply:
|
| 29 |
+
messages.append({"role": "assistant", "content": bot_reply})
|
| 30 |
+
|
|
|
|
|
|
|
| 31 |
messages.append({"role": "user", "content": message})
|
| 32 |
|
| 33 |
# Stream response
|
| 34 |
response = ""
|
| 35 |
+
for chunk in client.chat_completion(
|
| 36 |
+
messages=messages,
|
| 37 |
max_tokens=max_tokens,
|
|
|
|
| 38 |
temperature=temperature,
|
| 39 |
top_p=top_p,
|
| 40 |
+
stream=True
|
| 41 |
):
|
| 42 |
+
delta = chunk.choices[0].delta.content
|
| 43 |
+
if delta:
|
| 44 |
+
response += delta
|
| 45 |
+
yield response
|
| 46 |
|
| 47 |
+
# Gradio UI
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
+
fn=respond,
|
| 50 |
additional_inputs=[
|
| 51 |
+
gr.Textbox(value="You are a helpful assistant who only answers questions about Resilient Coders.", label="System message"),
|
| 52 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"),
|
| 53 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 54 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 55 |
],
|
| 56 |
+
title="Resilient Coders FAQ Chatbot",
|
| 57 |
+
description="Ask anything about the Resilient Coders bootcamp!"
|
| 58 |
)
|
| 59 |
|
|
|
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch()
|