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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py
2
  import gradio as gr
3
  import os
4
  from langchain_community.vectorstores import FAISS
@@ -9,42 +9,42 @@ from langchain.schema.output_parser import StrOutputParser
9
  from langchain.memory import ConversationBufferMemory
10
  from typing import List, Tuple
11
  import re
12
- # Environment variables for API keys
13
  TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
14
 
15
  class ChatBot:
16
  def __init__(self):
17
- # Initialize embeddings
18
  self.embeddings = TogetherEmbeddings(
19
  model="togethercomputer/m2-bert-80M-32k-retrieval",
20
  together_api_key=TOGETHER_API_KEY
21
  )
22
 
23
- # Load the pre-created FAISS index with embeddings
24
  self.vectorstore = FAISS.load_local(
25
  ".",
26
  embeddings=self.embeddings,
27
- allow_dangerous_deserialization=True # Only enable this if you trust the source of the index
28
  )
29
  self.retriever = self.vectorstore.as_retriever()
30
 
31
- # Initialize the model
32
  self.model = Together(
33
  model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
34
- temperature=0.4,
35
  max_tokens=128,
36
  top_k=30,
37
  together_api_key=TOGETHER_API_KEY
38
  )
39
 
40
- # Initialize memory
41
  self.memory = ConversationBufferMemory(
42
  return_messages=True,
43
  memory_key="chat_history",
44
  output_key="answer"
45
  )
46
 
47
- # Create the prompt template
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}
@@ -54,7 +54,7 @@ Javobni faqat matn shaklida bering, kod yoki ortiqcha belgilar kiritmang."""
54
 
55
  self.prompt = ChatPromptTemplate.from_template(self.template)
56
 
57
- # Create the chain
58
  self.chain = (
59
  {
60
  "context": self.retriever,
@@ -79,7 +79,7 @@ Javobni faqat matn shaklida bering, kod yoki ortiqcha belgilar kiritmang."""
79
  for tag in unwanted_tags:
80
  response = response.replace(tag, "")
81
 
82
- # Python kod snippetlarini olib tashlash
83
  response = re.sub(r"```.*?```", "", response, flags=re.DOTALL)
84
  response = re.sub(r"print\(.*?\)", "", response)
85
 
@@ -93,7 +93,7 @@ Javobni faqat matn shaklida bering, kod yoki ortiqcha belgilar kiritmang."""
93
  response = self.chain.invoke(message)
94
  clean_response = self.process_response(response)
95
 
96
- # Agar javob to'liq bo'lmasa yoki noto'g'ri bo'lsa, qayta urinib ko'rish
97
  if not clean_response or len(clean_response.split()) < 3:
98
  clean_response = "Kechirasiz, savolingizni tushunolmadim. Iltimos, batafsilroq savol bering."
99
 
@@ -107,7 +107,7 @@ Javobni faqat matn shaklida bering, kod yoki ortiqcha belgilar kiritmang."""
107
  self.memory.clear()
108
  return []
109
 
110
- # Create the Gradio interface
111
  def create_demo() -> gr.Interface:
112
  chatbot = ChatBot()
113
 
@@ -131,7 +131,7 @@ def create_demo() -> gr.Interface:
131
  clear = gr.Button("Yangi suhbat")
132
 
133
  def respond(message, chat_history):
134
- # Foydalanuvchi xabarini tozalash
135
  message = message.strip()
136
  if not message:
137
  return "", chat_history
 
1
+
2
  import gradio as gr
3
  import os
4
  from langchain_community.vectorstores import FAISS
 
9
  from langchain.memory import ConversationBufferMemory
10
  from typing import List, Tuple
11
  import re
12
+
13
  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,
27
+ allow_dangerous_deserialization=True
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}
 
54
 
55
  self.prompt = ChatPromptTemplate.from_template(self.template)
56
 
57
+
58
  self.chain = (
59
  {
60
  "context": self.retriever,
 
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
 
 
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
 
 
107
  self.memory.clear()
108
  return []
109
 
110
+
111
  def create_demo() -> gr.Interface:
112
  chatbot = ChatBot()
113
 
 
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