File size: 2,933 Bytes
9a2400c
796ecbf
9a2400c
 
d19140f
a14c267
 
 
 
 
9a2400c
d19140f
 
 
 
c5dc710
d19140f
c5dc710
f2267ee
 
d19140f
 
 
 
 
9a2400c
d19140f
90a6520
 
 
d19140f
 
 
 
 
 
 
9a2400c
 
90a6520
9a2400c
d19140f
 
9a2400c
 
 
d19140f
9a2400c
d19140f
 
 
 
9a2400c
d19140f
9a2400c
d19140f
9a2400c
d19140f
 
 
 
9a2400c
d19140f
 
9a2400c
 
 
d12d30f
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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()