OrifjonKenjayev commited on
Commit
ee5803c
·
verified ·
1 Parent(s): 261e4f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -25
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import gradio as gr
3
  import os
4
  from langchain_community.vectorstores import FAISS
@@ -14,13 +13,11 @@ TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
14
 
15
  class ChatBot:
16
  def __init__(self):
17
-
18
  self.embeddings = TogetherEmbeddings(
19
  model="togethercomputer/m2-bert-80M-32k-retrieval",
20
  together_api_key=TOGETHER_API_KEY
21
  )
22
 
23
-
24
  self.vectorstore = FAISS.load_local(
25
  ".",
26
  embeddings=self.embeddings,
@@ -28,33 +25,38 @@ class ChatBot:
28
  )
29
  self.retriever = self.vectorstore.as_retriever()
30
 
31
-
32
  self.model = Together(
33
  model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
34
- temperature=0.2,
35
  max_tokens=128,
36
  top_k=30,
37
  together_api_key=TOGETHER_API_KEY
38
  )
39
 
40
-
41
  self.memory = ConversationBufferMemory(
42
  return_messages=True,
43
  memory_key="chat_history",
44
  output_key="answer"
45
  )
46
 
47
-
48
- self.template = """Quyidagi kontekst va suhbat tarixiga asoslanib, savolga faqat matn ko'rinishida, kod yoki ortiqcha belgilarsiz, faqat o'zbek tilida tabiiy tarzda javob bering:
 
 
 
 
 
 
 
 
49
  Kontekst: {context}
50
  Suhbat Tarixi: {chat_history}
51
  Savol: {question}
52
- Javobni faqat matn shaklida bering, kod yoki ortiqcha belgilar kiritmang."""
53
 
54
-
 
55
  self.prompt = ChatPromptTemplate.from_template(self.template)
56
 
57
-
58
  self.chain = (
59
  {
60
  "context": self.retriever,
@@ -65,35 +67,28 @@ Javobni faqat matn shaklida bering, kod yoki ortiqcha belgilar kiritmang."""
65
  | self.model
66
  | StrOutputParser()
67
  )
68
-
69
  def get_chat_history(self) -> str:
70
- """Format chat history for the prompt"""
71
  messages = self.memory.load_memory_variables({})["chat_history"]
72
  return "\n".join([f"{m.type}: {m.content}" for m in messages])
73
 
74
- import re
75
-
76
  def process_response(self, response: str) -> str:
77
- """Clean up the response"""
78
  unwanted_tags = ["[INST]", "[/INST]", "<s>", "</s>"]
79
  for tag in unwanted_tags:
80
  response = response.replace(tag, "")
81
 
82
-
83
  response = re.sub(r"```.*?```", "", response, flags=re.DOTALL)
84
  response = re.sub(r"print\(.*?\)", "", response)
85
-
 
86
  return response.strip()
87
 
88
-
89
  def chat(self, message: str, history: List[Tuple[str, str]]) -> str:
90
- """Process a single chat message"""
91
  try:
92
  self.memory.chat_memory.add_user_message(message)
93
  response = self.chain.invoke(message)
94
  clean_response = self.process_response(response)
95
 
96
-
97
  if not clean_response or len(clean_response.split()) < 3:
98
  clean_response = "Kechirasiz, savolingizni tushunolmadim. Iltimos, batafsilroq savol bering."
99
 
@@ -103,11 +98,9 @@ Javobni faqat matn shaklida bering, kod yoki ortiqcha belgilar kiritmang."""
103
  return f"Xatolik yuz berdi: {str(e)}"
104
 
105
  def reset_chat(self) -> List[Tuple[str, str]]:
106
- """Reset the chat history"""
107
  self.memory.clear()
108
  return []
109
 
110
-
111
  def create_demo() -> gr.Interface:
112
  chatbot = ChatBot()
113
 
@@ -118,6 +111,7 @@ def create_demo() -> gr.Interface:
118
  chatbot_interface = gr.Chatbot(
119
  height=600,
120
  show_copy_button=True,
 
121
  )
122
 
123
  with gr.Row():
@@ -131,7 +125,6 @@ def create_demo() -> gr.Interface:
131
  clear = gr.Button("Yangi suhbat")
132
 
133
  def respond(message, chat_history):
134
-
135
  message = message.strip()
136
  if not message:
137
  return "", chat_history
@@ -140,9 +133,13 @@ def create_demo() -> gr.Interface:
140
  chat_history.append((message, bot_message))
141
  return "", chat_history
142
 
 
 
 
 
143
  submit.click(respond, [msg, chatbot_interface], [msg, chatbot_interface])
144
  msg.submit(respond, [msg, chatbot_interface], [msg, chatbot_interface])
145
- clear.click(lambda: chatbot.reset_chat(), None, chatbot_interface)
146
 
147
  return demo
148
 
 
 
1
  import gradio as gr
2
  import os
3
  from langchain_community.vectorstores import FAISS
 
13
 
14
  class ChatBot:
15
  def __init__(self):
 
16
  self.embeddings = TogetherEmbeddings(
17
  model="togethercomputer/m2-bert-80M-32k-retrieval",
18
  together_api_key=TOGETHER_API_KEY
19
  )
20
 
 
21
  self.vectorstore = FAISS.load_local(
22
  ".",
23
  embeddings=self.embeddings,
 
25
  )
26
  self.retriever = self.vectorstore.as_retriever()
27
 
 
28
  self.model = Together(
29
  model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
30
+ temperature=0.1,
31
  max_tokens=128,
32
  top_k=30,
33
  together_api_key=TOGETHER_API_KEY
34
  )
35
 
 
36
  self.memory = ConversationBufferMemory(
37
  return_messages=True,
38
  memory_key="chat_history",
39
  output_key="answer"
40
  )
41
 
42
+ # Updated prompt template with more specific instructions
43
+ self.template = """Quyidagi ko'rsatmalarga qat'iy rioya qiling:
44
+
45
+ 1. Faqat o'zbek tilida javob bering
46
+ 2. Faqat berilgan ma'lumotlar asosida javob bering
47
+ 3. Agar savol tushunarsiz bo'lsa yoki ma'lumot bo'lmasa, "Kechirasiz, bu haqida ma'lumotga ega emasman" deb javob bering
48
+ 4. O'zingizdan savol bermang
49
+ 5. Javobni takrorlamang
50
+ 6. Salomlashish uchun "Assalomu alaykum" yoki "Vaalaykum assalom" dan foydalaning
51
+
52
  Kontekst: {context}
53
  Suhbat Tarixi: {chat_history}
54
  Savol: {question}
 
55
 
56
+ Javob:"""
57
+
58
  self.prompt = ChatPromptTemplate.from_template(self.template)
59
 
 
60
  self.chain = (
61
  {
62
  "context": self.retriever,
 
67
  | self.model
68
  | StrOutputParser()
69
  )
70
+
71
  def get_chat_history(self) -> str:
 
72
  messages = self.memory.load_memory_variables({})["chat_history"]
73
  return "\n".join([f"{m.type}: {m.content}" for m in messages])
74
 
 
 
75
  def process_response(self, response: str) -> str:
 
76
  unwanted_tags = ["[INST]", "[/INST]", "<s>", "</s>"]
77
  for tag in unwanted_tags:
78
  response = response.replace(tag, "")
79
 
 
80
  response = re.sub(r"```.*?```", "", response, flags=re.DOTALL)
81
  response = re.sub(r"print\(.*?\)", "", response)
82
+ response = re.sub(r'\s+', ' ', response) # Remove multiple spaces
83
+
84
  return response.strip()
85
 
 
86
  def chat(self, message: str, history: List[Tuple[str, str]]) -> str:
 
87
  try:
88
  self.memory.chat_memory.add_user_message(message)
89
  response = self.chain.invoke(message)
90
  clean_response = self.process_response(response)
91
 
 
92
  if not clean_response or len(clean_response.split()) < 3:
93
  clean_response = "Kechirasiz, savolingizni tushunolmadim. Iltimos, batafsilroq savol bering."
94
 
 
98
  return f"Xatolik yuz berdi: {str(e)}"
99
 
100
  def reset_chat(self) -> List[Tuple[str, str]]:
 
101
  self.memory.clear()
102
  return []
103
 
 
104
  def create_demo() -> gr.Interface:
105
  chatbot = ChatBot()
106
 
 
111
  chatbot_interface = gr.Chatbot(
112
  height=600,
113
  show_copy_button=True,
114
+ value=[("", "Assalomu alaykum. Sizga qanday yordam bera olaman?")] # Initial greeting
115
  )
116
 
117
  with gr.Row():
 
125
  clear = gr.Button("Yangi suhbat")
126
 
127
  def respond(message, chat_history):
 
128
  message = message.strip()
129
  if not message:
130
  return "", chat_history
 
133
  chat_history.append((message, bot_message))
134
  return "", chat_history
135
 
136
+ def reset_chat():
137
+ chatbot.reset_chat()
138
+ return [("", "Assalomu alaykum. Sizga qanday yordam bera olaman?")]
139
+
140
  submit.click(respond, [msg, chatbot_interface], [msg, chatbot_interface])
141
  msg.submit(respond, [msg, chatbot_interface], [msg, chatbot_interface])
142
+ clear.click(reset_chat, None, chatbot_interface)
143
 
144
  return demo
145