R3BN commited on
Commit
e8711b5
·
verified ·
1 Parent(s): 23dd7a0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ # Load environment variables from .env file
7
+ load_dotenv()
8
+
9
+ # Groq API endpoint and key
10
+ GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
11
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
12
+
13
+ # Check if the API key is loaded
14
+ if not GROQ_API_KEY:
15
+ raise ValueError("GROQ_API_KEY not found in .env file")
16
+
17
+ def get_travel_guide_response(message, history):
18
+ # Prepare the conversation history
19
+ conversation = [
20
+ {"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."}
21
+ ]
22
+ for human, assistant in history:
23
+ conversation.append({"role": "user", "content": human})
24
+ conversation.append({"role": "assistant", "content": assistant})
25
+ conversation.append({"role": "user", "content": message})
26
+
27
+ # Make API request to Groq
28
+ headers = {
29
+ "Authorization": f"Bearer {GROQ_API_KEY}",
30
+ "Content-Type": "application/json"
31
+ }
32
+ data = {
33
+ "model": "llama3-8b-8192",
34
+ "messages": conversation,
35
+ "max_tokens": 500
36
+ }
37
+ response = requests.post(GROQ_API_URL, headers=headers, json=data)
38
+
39
+ if response.status_code == 200:
40
+ return response.json()["choices"][0]["message"]["content"]
41
+ else:
42
+ return f"Error: {response.status_code} - {response.text}"
43
+
44
+ # Custom CSS for better UI
45
+ custom_css = """
46
+ .container {
47
+ max-width: 800px !important;
48
+ margin: auto;
49
+ padding-top: 1.5rem;
50
+ }
51
+ .chat-message {
52
+ padding: 1rem;
53
+ border-radius: 0.5rem;
54
+ margin-bottom: 1rem;
55
+ box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
56
+ }
57
+ .user-message {
58
+ background-color: #e2e8f0;
59
+ }
60
+ .bot-message {
61
+ background-color: #edf2f7;
62
+ }
63
+ """
64
+
65
+ # Create Gradio interface with custom theme
66
+ theme = gr.themes.Default().set(
67
+ body_background_fill="*neutral_50",
68
+ block_background_fill="*neutral_100",
69
+ button_primary_background_fill="*primary_500",
70
+ button_primary_background_fill_dark="*primary_600",
71
+ )
72
+
73
+ with gr.Blocks(theme=theme, css=custom_css) as iface:
74
+ gr.Markdown("# Travel Guide Chatbot")
75
+ gr.Markdown("Ask me about travel destinations, itineraries, and local tips!")
76
+
77
+ chatbot = gr.Chatbot()
78
+ msg = gr.Textbox(label="Type your travel question here")
79
+ clear = gr.Button("Clear")
80
+
81
+ def user(user_message, history):
82
+ return "", history + [[user_message, None]]
83
+
84
+ def bot(history):
85
+ bot_message = get_travel_guide_response(history[-1][0], history[:-1])
86
+ history[-1][1] = bot_message
87
+ return history
88
+
89
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
90
+ bot, chatbot, chatbot
91
+ )
92
+ clear.click(lambda: None, None, chatbot, queue=False)
93
+
94
+ # Add dark mode toggle
95
+ with gr.Row():
96
+ dark_mode = gr.Checkbox(label="Dark Mode", value=False)
97
+
98
+ def toggle_dark_mode(dark):
99
+ if dark:
100
+ return gr.themes.Default(primary_hue="blue", neutral_hue="slate", text_size="lg").set(
101
+ body_background_fill="*neutral_900",
102
+ block_background_fill="*neutral_800",
103
+ button_primary_background_fill="*primary_600",
104
+ button_primary_background_fill_dark="*primary_700",
105
+ )
106
+ else:
107
+ return gr.themes.Default(primary_hue="blue", neutral_hue="slate", text_size="lg").set(
108
+ body_background_fill="*neutral_50",
109
+ block_background_fill="*neutral_100",
110
+ button_primary_background_fill="*primary_500",
111
+ button_primary_background_fill_dark="*primary_600",
112
+ )
113
+
114
+ dark_mode.change(toggle_dark_mode, dark_mode, None)
115
+
116
+ # Launch the interface
117
+ iface.launch()
118
+
119
+ if __name__ == "__main__":
120
+ launch_chatbot()