Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| from huggingface_hub import InferenceClient | |
| # Connect to Hugging Face model | |
| client = InferenceClient( | |
| "HuggingFaceH4/zephyr-7b-beta", | |
| token=os.environ.get("HF_API_TOKEN") | |
| ) | |
| # Example Q&A pairs about Resilient Coders | |
| rc_qa_examples = [ | |
| ("What is Resilient Coders?", "Resilient Coders is a nonprofit that trains young people of color for careers in tech."), | |
| ("Is the bootcamp free?", "Yes, the bootcamp is completely free and includes a stipend."), | |
| ("How long is the program?", "It usually runs about 20 weeks."), | |
| ("Do I need to know how to code?", "No prior experience is required. We train participants from the ground up."), | |
| ("Is it remote or in-person?", "The program may be remote"), | |
| ("What skills Resilient Coders Teach", "Our coders virtually meet, every day, and start off learning HTML and CSS, then take a deep dive into Javascript, Node, MongoDB and React. They build multiple projects each week and have the support of program team, mentors, and alumni during their coding journeys. Students also work on the skills that make the whole engineer during bootcamp. Our staff helps them build on their essential communication techniques, public speaking strategies, best time management practices, professional portfolios, and interviewing skills."), | |
| ] | |
| # Main response function | |
| def respond(message, history, system_message, max_tokens, temperature, top_p): | |
| # Format prompt | |
| messages = [{"role": "system", "content": system_message}] | |
| for q, a in rc_qa_examples: | |
| messages.append({"role": "user", "content": q}) | |
| messages.append({"role": "assistant", "content": a}) | |
| for user_msg, bot_reply in history: | |
| if user_msg: | |
| messages.append({"role": "user", "content": user_msg}) | |
| if bot_reply: | |
| messages.append({"role": "assistant", "content": bot_reply}) | |
| messages.append({"role": "user", "content": message}) | |
| # Stream response | |
| response = "" | |
| for chunk in client.chat_completion( | |
| messages=messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| stream=True | |
| ): | |
| delta = chunk.choices[0].delta.content | |
| if delta: | |
| response += delta | |
| yield response | |
| # Gradio UI | |
| demo = gr.ChatInterface( | |
| fn=respond, | |
| additional_inputs=[ | |
| gr.Textbox(value="You are a helpful assistant who only answers questions about Resilient Coders.", label="System message"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"), | |
| gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"), | |
| ], | |
| title="Resilient Coders FAQ Chatbot", | |
| description="Ask anything about the Resilient Coders bootcamp!" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |