File size: 4,056 Bytes
25701c3
f753875
3f5c401
25701c3
f753875
 
 
 
a99258b
f753875
a99258b
f753875
a99258b
f753875
 
 
 
 
 
 
 
 
 
3f5c401
a99258b
 
 
 
 
3f5c401
 
 
 
 
 
 
 
 
a99258b
3f5c401
 
 
 
a99258b
3f5c401
 
 
 
a99258b
 
25701c3
3f5c401
 
 
 
 
 
 
 
f753875
3f5c401
 
 
 
 
 
 
 
f753875
3f5c401
 
f753875
3f5c401
 
f753875
3f5c401
 
 
f753875
 
 
 
 
 
 
3f5c401
f753875
 
 
 
 
 
 
 
3f5c401
f753875
 
 
 
 
 
 
 
 
 
3f5c401
 
 
 
 
 
f753875
3f5c401
 
 
 
 
 
 
 
 
 
 
f753875
3f5c401
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import gradio as gr
import random
from huggingface_hub import InferenceClient

def match_europe(scores):
    a = scores.count("A")
    b = scores.count("B")
    c = scores.count("C")
    if a >= 5:
        return "Rome"
    elif b >=5:
        return "Paris"
    elif c >= 5:
        return "London"
    else:
        if  a > b and a > c:
            return "Rome"
        elif b > a and b > c:
            return "Paris"
        elif c > a and c > b:
            return "London"
        else:
            return "Rome"
questions_europe = [
    ("1. What kind of weather do you prefer?", [
        ("A", "Warm and sunny"),
        ("B", "Mild and a bit cloudy"),
        ("C", "Cool with a chance of rain")
    ]),
    ("2. What kind of shopping excites you the most?", [
        ("A", "Handmade crafts and local markets."),
        ("B", "Perfumeries, boutiques, and high fashion."),
        ("C", "Bookstores, vinyl shops, and vintage finds.")
    ]),
    ("3. How would your friends describe you?", [
        ("A", "Passionate and expressive."),
        ("B", "Thoughtful and stylish."),
        ("C", "Clever and witty.")
    ]),
    ("4. What does your ideal travel day not include?", [
        ("A", "Rushing from place to place."),
        ("B", "Group tours and loud crowds."),
        ("C", "Skipping museams and rainy walks.")
    ]),
    ("5. You find a secret key in your room. What do you hope it unlocks?", [
        ("A", "A hidden underground ruin."),
        ("B", "A secret rooftop garden with a view."),
        ("C", "A private library or secret pub.")
    ])
]

def create_quiz(questions, match_func):
    answers = []
    with gr.Column():
        for q, opts in questions:
            choices = [opt[1] for opt in opts]
            answers.append(gr.Radio(choices=choices, label=q))
        result = gr.Textbox(label="In Europe you should travel to...")
        btn = gr.Button("Find My Match")

    def evaluate(*vals):
        codes = []
        for i, val in enumerate(vals):
            if val is None:
                codes.append("C")
            else:
                codes.append(next(opt[0] for opt in questions[i][1] if opt[1] == val))
        return match_func(codes)

    btn.click(evaluate, inputs=answers, outputs=result)
    return answers, result, btn

with gr.Blocks() as demo:
    gr.Markdown("Where Should I Travel Quiz?")

    with gr.Tabs():
        with gr.TabItem("Europe"):
            create_quiz(questions_europe, match_europe)


client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
# change the LLM

def respond(message, history):

    messages = [{"role": "system", "content": "You are a chatbot that helps people plan their trips to make it easier for them. You usually try to give shorter responses unless the customer asks for an itinerary."}]
    # change the personality of the chatbot
    if history:
        messages.extend(history)
        
    messages.append({"role" : "user", "content" : message})

    response = ""
    for message in client.chat_completion(
        messages, max_tokens = 4096, stream=True
        #temperature= .1, top_p= 0.7)
        # max tokens = change the length of the response 
        # temp = between 0-2
        # top-p =  between 0-1
    ):
        token = message.choices[0].delta.content
        if token:
            response += token
        yield response
    
with gr.Blocks() as app:
    gr.Markdown("# 🌍 Where Should I Travel?")

    with gr.Tabs():
        with gr.TabItem("Europe Quiz"):
            create_quiz(questions_europe, match_europe)

        with gr.TabItem("Travel Chatbot"):
            gr.ChatInterface(
                respond,
                type="messages",
                title="Travia",
                examples=[
                    "I would like to travel to a place, but I don't know how to plan it",
                    "I need to budget for my trip.",
                    "I want to learn more about the language, food, and culture of the place I'm traveling to."
                ]
            )

app.launch(debug=True)