File size: 3,964 Bytes
e8711b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa35d87
e8711b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa35d87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8711b5
 
 
 
 
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
import gradio as gr
import requests
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Groq API endpoint and key
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

# Check if the API key is loaded
if not GROQ_API_KEY:
    raise ValueError("GROQ_API_KEY not found in .env file")

def get_travel_guide_response(message, history):
    # Prepare the conversation history
    conversation = [
        {"role": "system", "content": "You are a travel guide chatbot. Only provide information and answer questions strictly related to travel, destinations, itineraries, local tips, and cultural information. If a question is not related to travel, politely ask the user to only ask travel-related questions."}
    ]
    for human, assistant in history:
        conversation.append({"role": "user", "content": human})
        conversation.append({"role": "assistant", "content": assistant})
    conversation.append({"role": "user", "content": message})

    # Make API request to Groq
    headers = {
        "Authorization": f"Bearer {GROQ_API_KEY}",
        "Content-Type": "application/json"
    }
    data = {
        "model": "mixtral-8x7b-32768",
        "messages": conversation,
        "max_tokens": 500
    }
    response = requests.post(GROQ_API_URL, headers=headers, json=data)
    
    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        return f"Error: {response.status_code} - {response.text}"

# Custom CSS for better UI
custom_css = """
.container {
    max-width: 800px !important;
    margin: auto;
    padding-top: 1.5rem;
}
.chat-message {
    padding: 1rem;
    border-radius: 0.5rem;
    margin-bottom: 1rem;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}
.user-message {
    background-color: #e2e8f0;
}
.bot-message {
    background-color: #edf2f7;
}
"""

# Create Gradio interface with custom theme
theme = gr.themes.Default().set(
    body_background_fill="*neutral_50",
    block_background_fill="*neutral_100",
    button_primary_background_fill="*primary_500",
    button_primary_background_fill_dark="*primary_600",
)

with gr.Blocks(theme=theme, css=custom_css) as iface:
    gr.Markdown("# Travel Guide Chatbot")
    gr.Markdown("Ask me about travel destinations, itineraries, and local tips!")
    
    chatbot = gr.Chatbot()
    msg = gr.Textbox(label="Type your travel question here")
    clear = gr.Button("Clear")

    def user(user_message, history):
        return "", history + [[user_message, None]]

    def bot(history):
        bot_message = get_travel_guide_response(history[-1][0], history[:-1])
        history[-1][1] = bot_message
        return history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    clear.click(lambda: None, None, chatbot, queue=False)

    # Add dark mode toggle
    with gr.Row():
        dark_mode = gr.Checkbox(label="Dark Mode", value=False)
    
    def toggle_dark_mode(dark):
        if dark:
            return gr.themes.Default(primary_hue="blue", neutral_hue="slate", text_size="lg").set(
                body_background_fill="*neutral_900",
                block_background_fill="*neutral_800",
                button_primary_background_fill="*primary_600",
                button_primary_background_fill_dark="*primary_700",
            )
        else:
            return gr.themes.Default(primary_hue="blue", neutral_hue="slate", text_size="lg").set(
                body_background_fill="*neutral_50",
                block_background_fill="*neutral_100",
                button_primary_background_fill="*primary_500",
                button_primary_background_fill_dark="*primary_600",
            )
    
    dark_mode.change(toggle_dark_mode, dark_mode, None)

# Launch the interface
iface.launch()

if __name__ == "__main__":
    launch_chatbot()