alapl063 commited on
Commit
b533400
·
verified ·
1 Parent(s): fa94853

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -46
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import os
2
  import gradio as gr
3
- from groq import Groq
4
  import langdetect
5
  import requests
6
  import random
7
 
8
  GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
9
- client = Groq(api_key=GROQ_API_KEY)
10
 
11
  def detect_language(text):
12
  """Detects whether the input text is in French or English."""
@@ -36,44 +36,37 @@ def handle_button_click(button_text):
36
  return "Plan a 1-week trip for me."
37
  return ""
38
 
39
- def chat_with_bot(message, history):
40
- """Handles user queries and maintains chat memory."""
 
 
 
 
 
 
 
41
 
42
- if message in ["Trip recommendations", "Send me somewhere!", "1 week planned vacations"]:
43
- message = handle_button_click(message)
44
-
45
- language = detect_language(message)
46
-
47
- system_message = {
48
- "en": "You help users plan trips. Keep responses short and clear.",
49
- "fr": "Vous aidez les utilisateurs à planifier des voyages. Réponses courtes et simples."
50
- }
51
-
52
- messages = [{"role": "system", "content": system_message[language]}]
53
-
54
- for user_msg, bot_msg in history:
55
- messages.append({"role": "user", "content": user_msg})
56
- messages.append({"role": "assistant", "content": bot_msg})
57
 
58
  messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- if any(keyword in message.lower() for keyword in ["book a flight", "hotel reservation", "car rental", "book a room"]):
61
- link = find_reservation_link(message)
62
- return history + [(message, f"Here's a link that might help: {link}")], ""
63
-
64
- response = client.chat.completions.create(
65
- model="llama-3.3-70b-versatile",
66
- messages=messages,
67
- temperature=0.7,
68
- max_tokens=1024,
69
- top_p=1
70
- )
71
-
72
- bot_reply = response.choices[0].message.content
73
- history.append((message, bot_reply))
74
- return history, ""
75
-
76
- # Gradio interface with memory
77
  with gr.Blocks(css="""
78
  body { background-color: #A9B5DF; }
79
  .gradio-container { background-color: #A9B5DF; }
@@ -83,7 +76,7 @@ with gr.Blocks(css="""
83
  """) as demo:
84
  gr.Markdown("# 🌍 ***FlightAI - Your Travel Assistant - Votre Assistant de Voyage*** ✈️\n")
85
 
86
- chatbot = gr.Chatbot(type="messages") # ✅ Fixed deprecated warning
87
  chatbot.value = [
88
  {"role": "assistant", "content": "Hey! I'm FlightAI. Tell me your travel plans or pick a button below!\n\nSalut! Je suis FlightAI. Dis-moi ton projet de voyage ou choisis un bouton ci-dessous!"}
89
  ]
@@ -95,14 +88,20 @@ with gr.Blocks(css="""
95
 
96
  user_input = gr.Textbox(placeholder="Type your travel question here...")
97
 
98
- def button_click(btn_text):
99
- response, _ = chat_with_bot(btn_text, chatbot.value)
100
- return response, ""
101
-
102
- btn1.click(button_click, inputs=[btn1], outputs=[chatbot, user_input])
103
- btn2.click(button_click, inputs=[btn2], outputs=[chatbot, user_input])
104
- btn3.click(button_click, inputs=[btn3], outputs=[chatbot, user_input])
105
- user_input.submit(chat_with_bot, inputs=[user_input, chatbot], outputs=[chatbot, user_input])
 
 
 
 
 
 
 
106
 
107
- if __name__ == "__main__":
108
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
  import langdetect
5
  import requests
6
  import random
7
 
8
  GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
9
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
11
  def detect_language(text):
12
  """Detects whether the input text is in French or English."""
 
36
  return "Plan a 1-week trip for me."
37
  return ""
38
 
39
+ def respond(
40
+ message,
41
+ history: list[tuple[str, str]],
42
+ system_message,
43
+ max_tokens,
44
+ temperature,
45
+ top_p,
46
+ ):
47
+ messages = [{"role": "system", "content": system_message}]
48
 
49
+ for val in history:
50
+ if val[0]:
51
+ messages.append({"role": "user", "content": val[0]})
52
+ if val[1]:
53
+ messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
 
 
 
 
 
54
 
55
  messages.append({"role": "user", "content": message})
56
+
57
+ response = ""
58
+
59
+ for message in client.chat_completion(
60
+ messages,
61
+ max_tokens=max_tokens,
62
+ stream=True,
63
+ temperature=temperature,
64
+ top_p=top_p,
65
+ ):
66
+ token = message.choices[0].delta.content
67
+ response += token
68
+ yield response
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  with gr.Blocks(css="""
71
  body { background-color: #A9B5DF; }
72
  .gradio-container { background-color: #A9B5DF; }
 
76
  """) as demo:
77
  gr.Markdown("# 🌍 ***FlightAI - Your Travel Assistant - Votre Assistant de Voyage*** ✈️\n")
78
 
79
+ chatbot = gr.Chatbot(type="messages")
80
  chatbot.value = [
81
  {"role": "assistant", "content": "Hey! I'm FlightAI. Tell me your travel plans or pick a button below!\n\nSalut! Je suis FlightAI. Dis-moi ton projet de voyage ou choisis un bouton ci-dessous!"}
82
  ]
 
88
 
89
  user_input = gr.Textbox(placeholder="Type your travel question here...")
90
 
91
+ demo = gr.ChatInterface(
92
+ respond,
93
+ additional_inputs=[
94
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
95
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
96
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
97
+ gr.Slider(
98
+ minimum=0.1,
99
+ maximum=1.0,
100
+ value=0.95,
101
+ step=0.05,
102
+ label="Top-p (nucleus sampling)",
103
+ ),
104
+ ],
105
+ )
106
 
 
107
  demo.launch()