Aca792 commited on
Commit
ea80bdb
·
1 Parent(s): 69235f1

Cursor Merge

Browse files
Files changed (4) hide show
  1. app.py +39 -34
  2. requirements.txt +11 -2
  3. simple_turbot.py +146 -0
  4. test_agent.py +40 -0
app.py CHANGED
@@ -1,20 +1,14 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- import os
4
-
5
- # Load token from environment variable
6
- token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
7
- if not token:
8
- raise ValueError("HUGGINGFACEHUB_API_TOKEN environment variable not set.")
9
-
10
- # Instantiate the inference client with token from env
11
- client = InferenceClient(
12
- "HuggingFaceH4/zephyr-7b-beta",
13
- token=token
14
- )
15
 
16
  def respond(
17
- message: str,
18
  history: list[tuple[str, str]],
19
  system_message,
20
  max_tokens,
@@ -23,37 +17,48 @@ def respond(
23
  ):
24
  messages = [{"role": "system", "content": system_message}]
25
 
26
- # Convert Gradio history tuples into OpenAI-style messages
27
- for user_msg, assistant_msg in history:
28
- messages.append({"role": "user", "content": user_msg})
29
- messages.append({"role": "assistant", "content": assistant_msg})
 
30
 
31
- # Append latest user message
32
  messages.append({"role": "user", "content": message})
33
 
34
- try:
35
- result = client.chat_completion(
36
- messages=messages,
37
- max_tokens=max_tokens,
38
- stream=False,
39
- temperature=temperature,
40
- top_p=top_p,
41
- )
42
- yield result.choices[0].message.content
43
- except Exception as e:
44
- yield f"Error: {str(e)}"
45
-
46
- # Build the Gradio interface
 
 
 
 
 
47
  demo = gr.ChatInterface(
48
  respond,
49
  additional_inputs=[
50
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
54
  ],
55
- type="messages"
56
  )
57
 
 
58
  if __name__ == "__main__":
59
  demo.launch()
 
1
  import gradio as gr
2
+ from huggingface_hub.inference._client import InferenceClient
3
+
4
+ """
5
+ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
+ """
7
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
+
 
 
 
 
 
 
9
 
10
  def respond(
11
+ message,
12
  history: list[tuple[str, str]],
13
  system_message,
14
  max_tokens,
 
17
  ):
18
  messages = [{"role": "system", "content": system_message}]
19
 
20
+ for val in history:
21
+ if val[0]:
22
+ messages.append({"role": "user", "content": val[0]})
23
+ if val[1]:
24
+ messages.append({"role": "assistant", "content": val[1]})
25
 
 
26
  messages.append({"role": "user", "content": message})
27
 
28
+ response = ""
29
+
30
+ for message in client.chat_completion(
31
+ messages,
32
+ max_tokens=max_tokens,
33
+ stream=True,
34
+ temperature=temperature,
35
+ top_p=top_p,
36
+ ):
37
+ token = message.choices[0].delta.content
38
+ if token:
39
+ response += token
40
+ yield response
41
+
42
+
43
+ """
44
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
+ """
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
+ gr.Slider(
53
+ minimum=0.1,
54
+ maximum=1.0,
55
+ value=0.95,
56
+ step=0.05,
57
+ label="Top-p (nucleus sampling)",
58
+ ),
59
  ],
 
60
  )
61
 
62
+
63
  if __name__ == "__main__":
64
  demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,11 @@
1
- huggingface_hub==0.25.2
2
- gradio
 
 
 
 
 
 
 
 
 
 
1
+ gradio==4.31.0
2
+ langchain==0.2.1
3
+ langgraph==0.0.62
4
+ huggingface_hub==0.23.0
5
+ langchain-huggingface==0.0.3
6
+ faiss-cpu==1.8.0
7
+ langchain_community==0.2.1
8
+ python-dotenv==1.0.1
9
+ tiktoken==0.7.0
10
+ langchain-text-splitters==0.2.0
11
+ sentence-transformers==2.7.0
simple_turbot.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Simple TurBot implementation that works with Hugging Face.
4
+ """
5
+
6
+ import os
7
+ from typing import List
8
+ from dotenv import load_dotenv
9
+ from huggingface_hub.inference._client import InferenceClient
10
+ from langchain_community.vectorstores import FAISS
11
+ from langchain_huggingface import HuggingFaceEmbeddings
12
+
13
+ # Load environment variables
14
+ load_dotenv()
15
+
16
+ # Initialize the LLM
17
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
18
+
19
+ # Load the vector store
20
+ DB_FAISS_PATH = "vectorstore/db_faiss"
21
+
22
+ def search_travel_packages(query: str) -> str:
23
+ """
24
+ Search for travel packages based on the user's query.
25
+ """
26
+ try:
27
+ # Load the vector store
28
+ embeddings = HuggingFaceEmbeddings(
29
+ model_name='sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2',
30
+ model_kwargs={'device': 'cpu'}
31
+ )
32
+
33
+ if os.path.exists(DB_FAISS_PATH):
34
+ vectorstore = FAISS.load_local(DB_FAISS_PATH, embeddings)
35
+
36
+ # Search for relevant documents
37
+ docs = vectorstore.similarity_search(query, k=3)
38
+
39
+ # Combine the relevant information
40
+ context = "\n\n".join([doc.page_content for doc in docs])
41
+ return f"Found relevant travel information:\n\n{context}"
42
+ else:
43
+ return "Travel package database not found. Please ensure the vector store has been created."
44
+ except Exception as e:
45
+ return f"Error searching travel packages: {str(e)}"
46
+
47
+ def should_search_travel_info(message: str) -> bool:
48
+ """Decide whether to search for travel information based on the message content."""
49
+ message_lower = message.lower()
50
+
51
+ # Keywords that indicate we should search for travel information
52
+ travel_keywords = [
53
+ "destinacija", "let", "hotel", "putovanje", "mediteran", "grčka", "italija",
54
+ "turska", "budžet", "porodica", "all inclusive", "preporučite", "ponuda",
55
+ "destination", "flight", "travel", "mediterranean", "greece", "italy",
56
+ "turkey", "budget", "family", "recommend", "offer"
57
+ ]
58
+
59
+ return any(keyword in message_lower for keyword in travel_keywords)
60
+
61
+ def chat_with_turbot(message: str, history: List[List[str]]) -> str:
62
+ """
63
+ Main function to chat with TurBot.
64
+ """
65
+ # Convert history to the format expected by our LLM
66
+ messages = []
67
+ for human, ai in history:
68
+ if human:
69
+ messages.append({"role": "user", "content": human})
70
+ if ai:
71
+ messages.append({"role": "assistant", "content": ai})
72
+
73
+ # Add the current message
74
+ messages.append({"role": "user", "content": message})
75
+
76
+ # Check if we should search for travel information
77
+ context = ""
78
+ if should_search_travel_info(message):
79
+ context = search_travel_packages(message)
80
+
81
+ # Prepare the system message
82
+ if context:
83
+ system_message = f"""Ti si TurBot, digitalni asistent za turističku agenciju.
84
+ Koristi sledeće informacije o putovanjima da odgovoriš na pitanje korisnika:
85
+
86
+ {context}
87
+
88
+ Odgovori na srpskom jeziku i budi koristan i prijateljski nastrojen."""
89
+ else:
90
+ system_message = """Ti si TurBot, digitalni asistent za turističku agenciju.
91
+ Odgovori na srpskom jeziku i budi koristan i prijateljski nastrojen.
92
+ Ako ne znaš odgovor na pitanje, preporuči korisniku da pita o putovanjima, letovima, hotelima ili destinacijama."""
93
+
94
+ # Add system message to the beginning
95
+ messages.insert(0, {"role": "system", "content": system_message})
96
+
97
+ try:
98
+ # Generate response
99
+ response = ""
100
+ for message_chunk in client.chat_completion(
101
+ messages,
102
+ stream=True,
103
+ temperature=0.7,
104
+ top_p=0.95,
105
+ ):
106
+ token = message_chunk.choices[0].delta.content
107
+ if token:
108
+ response += token
109
+
110
+ return response
111
+ except Exception as e:
112
+ return f"Izvinjavam se, došlo je do greške: {str(e)}"
113
+
114
+ def test_turbot():
115
+ """Test the TurBot agent with some sample questions."""
116
+
117
+ print("🤖 Testing TurBot - Digitalni Asistent za Turističku Agenciju")
118
+ print("=" * 60)
119
+
120
+ test_questions = [
121
+ "Zdravo! Ko ste vi?",
122
+ "Koje letnje ponude imate za Mediteran?",
123
+ "Tražim putovanje u Grčku za porodicu od četiri osobe.",
124
+ "Koji all-inclusive paketi za Tursku su dostupni?"
125
+ ]
126
+
127
+ history = []
128
+
129
+ for question in test_questions:
130
+ print(f"\n👤 Korisnik: {question}")
131
+ print("-" * 40)
132
+
133
+ try:
134
+ response = chat_with_turbot(question, history)
135
+ print(f"🤖 TurBot: {response}")
136
+
137
+ # Add to history for next iteration
138
+ history.append([question, response])
139
+
140
+ except Exception as e:
141
+ print(f"❌ Greška: {str(e)}")
142
+
143
+ print("-" * 40)
144
+
145
+ if __name__ == "__main__":
146
+ test_turbot()
test_agent.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Simple test script for TurBot agent.
4
+ """
5
+
6
+ from src.agent import chat_with_turbot
7
+
8
+ def test_turbot():
9
+ """Test the TurBot agent with some sample questions."""
10
+
11
+ print("🤖 Testing TurBot - Digitalni Asistent za Turističku Agenciju")
12
+ print("=" * 60)
13
+
14
+ test_questions = [
15
+ "Zdravo! Ko ste vi?",
16
+ "Koje letnje ponude imate za Mediteran?",
17
+ "Tražim putovanje u Grčku za porodicu od četiri osobe.",
18
+ "Koji all-inclusive paketi za Tursku su dostupni?"
19
+ ]
20
+
21
+ history = []
22
+
23
+ for question in test_questions:
24
+ print(f"\n👤 Korisnik: {question}")
25
+ print("-" * 40)
26
+
27
+ try:
28
+ response = chat_with_turbot(question, history)
29
+ print(f"🤖 TurBot: {response}")
30
+
31
+ # Add to history for next iteration
32
+ history.append([question, response])
33
+
34
+ except Exception as e:
35
+ print(f"❌ Greška: {str(e)}")
36
+
37
+ print("-" * 40)
38
+
39
+ if __name__ == "__main__":
40
+ test_turbot()