|
|
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") |
|
|
|
|
|
|
|
|
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."}] |
|
|
|
|
|
if history: |
|
|
messages.extend(history) |
|
|
|
|
|
messages.append({"role" : "user", "content" : message}) |
|
|
|
|
|
response = "" |
|
|
for message in client.chat_completion( |
|
|
messages, max_tokens = 4096, stream=True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
): |
|
|
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) |
|
|
|