GIGAParviz commited on
Commit
32a4077
·
verified ·
1 Parent(s): 8c67434

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +208 -183
app.py CHANGED
@@ -1,117 +1,79 @@
1
  import os
2
  import re
3
- import sqlite3
4
- from datetime import datetime
5
  from pypdf import PdfReader
6
  import gradio as gr
 
7
  from langchain_groq import ChatGroq
8
  from langchain_huggingface import HuggingFaceEmbeddings
9
- from langchain.vectorstores import Chroma
10
  from langchain_core.documents import Document
11
  from langchain_text_splitters import RecursiveCharacterTextSplitter
 
 
 
 
 
 
12
 
13
  models = ["deepseek-r1-distill-llama-70b", "llama-3.3-70b-versatile", "gemma2-9b-it"]
14
  default_model = models[0]
15
 
16
- class DatabaseManager:
17
- def __init__(self, db_name="chat_history.db"):
18
- self.conn = sqlite3.connect(db_name)
19
- self._create_tables()
20
- def _create_tables(self):
21
- cursor = self.conn.cursor()
22
- cursor.execute(
23
- '''CREATE TABLE IF NOT EXISTS chat_summaries
24
- (id INTEGER PRIMARY KEY AUTOINCREMENT,
25
- timestamp DATETIME,
26
- summary TEXT,
27
- model_used TEXT,
28
- token_count INT)'''
29
- )
30
- self.conn.commit()
31
- def save_summary(self, summary_data):
32
- try:
33
- cursor = self.conn.cursor()
34
- cursor.execute(
35
- '''INSERT INTO chat_summaries
36
- (timestamp, summary, model_used, token_count)
37
- VALUES (?, ?, ?, ?)''',
38
- (datetime.now(),
39
- summary_data['summary'],
40
- summary_data['model'],
41
- summary_data['tokens'])
42
- )
43
- self.conn.commit()
44
- return True
45
- except Exception as e:
46
- print(f"Database error: {str(e)}")
47
- return False
48
- def load_summaries(self, limit=5):
49
- cursor = self.conn.cursor()
50
- cursor.execute(
51
- "SELECT summary FROM chat_summaries ORDER BY id DESC LIMIT ?",
52
- (limit,)
53
- )
54
- rows = cursor.fetchall()
55
- return "\n".join([row[0] for row in rows])
56
-
57
- class AICore:
58
- def __init__(self):
59
- self.embeddings = HuggingFaceEmbeddings(model_name="heydariAI/persian-embeddings")
60
- self.vector_store = Chroma(embedding_function=self.embeddings)
61
- self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
62
- self.chat_history = []
63
- self.price_per_token = 0.00001
64
- self.api_key = "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"
65
- self.model = ChatGroq(api_key=self.api_key, model_name=default_model)
66
- self.db = DatabaseManager()
67
- def _init_model(self, model_name):
68
- if self.model.model_name != model_name:
69
- self.model = ChatGroq(api_key=self.api_key, model_name=model_name)
70
- def summarize_chat(self):
71
- chat_text = "\n".join([f"پرسش: {q}\nپاسخ: {a}" for q, a in self.chat_history])
72
- summary_prompt = f"یک خلاصه کوتاه از مکالمه زیر ارائه کن:\n\n{chat_text}\n\nخلاصه:"
73
- summary_response = self.model.invoke(summary_prompt)
74
- return summary_response.content
75
- def process_file(self, file_obj):
76
- if not file_obj:
77
- return None
78
- file_path = file_obj.name if hasattr(file_obj, "name") else file_obj
79
- file_extension = os.path.splitext(file_path)[1].lower()
80
- try:
81
- if file_extension == ".pdf":
82
- reader = PdfReader(file_path)
83
- file_text = "\n".join(page.extract_text() for page in reader.pages)
84
- elif file_extension == ".txt":
85
- with open(file_path, "r", encoding="utf-8") as f:
86
- file_text = f.read()
87
- else:
88
- raise ValueError(f"Unsupported file format: {file_extension}")
89
- file_docs = [Document(page_content=file_text, metadata={"source": "uploaded_file"})]
90
- file_splits = self.text_splitter.split_documents(file_docs)
91
- self.vector_store.add_documents(file_splits)
92
- return file_text
93
- except Exception as e:
94
- raise RuntimeError(f"Error processing file: {str(e)}")
95
- def count_tokens(self, text):
96
- return len(text.split())
97
- def calculate_price(self, input_text, output_text):
98
- input_tokens = self.count_tokens(input_text)
99
- output_tokens = self.count_tokens(output_text)
100
- total_tokens = input_tokens + output_tokens
101
- total_price = total_tokens * self.price_per_token
102
- return total_tokens, f"{total_price:.6f} دلار"
103
- def remove_think_sections(self, response_text):
104
- return re.sub(r"<think>.*?</think>", "", response_text, flags=re.DOTALL)
105
- def filter_to_persian(self, text):
106
- return re.sub(r'[^\u0600-\u06FF\s\.,؛؟!٪،0-9]', '', text)
107
- def answer_query(self, query, file_obj, summarize, tone, model_name, creativity,
108
- keywords, language, response_length, welcome_message, exclusion_words):
109
- self._init_model(model_name)
110
- if file_obj:
111
- self.process_file(file_obj)
112
  search_query = f"{keywords} {query}" if keywords else query
113
- retrieved_docs = self.vector_store.similarity_search(search_query, k=3)
114
  knowledge = "\n\n".join(doc.page_content for doc in retrieved_docs)
 
115
  tone_prompts = {
116
  "رسمی": "پاسخ را با لحنی رسمی و مودبانه ارائه کن.",
117
  "محاوره‌ای": "پاسخ را به صورت دوستانه ارائه کن.",
@@ -119,8 +81,7 @@ class AICore:
119
  "طنزآمیز": "پاسخ را با لحنی طنزآمیز ارائه کن.",
120
  }
121
  tone_instruction = tone_prompts.get(tone, f"پاسخ را به زبان {language} ارائه کن.")
122
- language_instruction = (f"پاسخ را فقط به زبان {language} ارائه کن و از زبان دیگری استفاده نکن مگر آنکه بخواهی کد بنویسی "
123
- f"که در آن صورت فقط از زبان انگلیسی استفاده کن مگر اینکه کاربر از تو درخواست کند از زبان دیگری استفاده بکنی و از زبان چینی استفاده نکن.") if language else ""
124
  if response_length == "کوتاه":
125
  length_instruction = "پاسخ را به صورت مختصر ارائه کن."
126
  elif response_length == "بلند":
@@ -128,102 +89,166 @@ class AICore:
128
  else:
129
  length_instruction = ""
130
  exclusion_instruction = f"از کلمات زیر در پاسخ استفاده نکن: {exclusion_words}" if exclusion_words else ""
 
131
  prompt = (
132
- f"شما Parviz Mind هستید، یک دستیار هوش مصنوعی ساخته شده توسط امیرمهدی پرویز دانشجو دانشگاه صنعتی کرمانشاه "
133
  f"{tone_instruction} {language_instruction} {length_instruction} {exclusion_instruction}\n\n"
134
  )
135
- if welcome_message and not self.chat_history:
 
136
  prompt = f"{welcome_message}\n\n" + prompt
137
- if self.chat_history:
138
- conversation_history = "\n".join([f"پرسش: {q}\nپاسخ: {a}" for q, a in self.chat_history])
 
139
  prompt = f"{conversation_history}\n\n" + prompt
 
140
  prompt += f"اطلاعات مرتبط:\n{knowledge}\n\nسوال: {query}\nپاسخ:"
141
- response = self.model.invoke(prompt, temperature=creativity)
142
- cleaned_response = self.remove_think_sections(response.content)
143
- cleaned_response = self.filter_to_persian(cleaned_response)
144
- self.chat_history.append((query, cleaned_response))
145
- total_tokens, price = self.calculate_price(prompt, cleaned_response)
146
- summary_text = self.summarize_chat() if summarize else "خلاصه‌سازی غیرفعال است."
147
- if summarize and summary_text != "خلاصه‌سازی غیرفعال است.":
148
- self.db.save_summary({
149
- 'summary': summary_text,
150
- 'model': model_name,
151
- 'tokens': total_tokens
152
- })
153
- return cleaned_response, summary_text, total_tokens, price
154
- def clear_history(self):
155
- self.chat_history = []
156
- return self.chat_history
157
-
158
- class ChatInterface:
159
- def __init__(self, ai_core: AICore):
160
- self.ai = ai_core
161
- self._create_interface()
162
- def _create_interface(self):
163
- with gr.Blocks() as self.interface:
164
- gr.Markdown("## 🤖 Parviz Mind")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  gr.Markdown("**یک فایل (PDF یا TXT) آپلود کنید و سوال خود را بپرسید.**")
166
- self.chatbot = gr.Chatbot(label="💬 تاریخچه چت")
167
- self.query_input = gr.Textbox(label=" سوال خود را وارد کنید")
168
- self.summarize_checkbox = gr.Checkbox(label="📌 خلاصه‌ساز را فعال کن")
169
- self.submit_button = gr.Button("🚀 ارسال")
170
- self.del_button = gr.Button("🗑 پاک کردن حافظه")
171
- self.file_input = gr.File(label="📂 آپلود فایل", file_types=[".pdf", ".txt"])
172
- with gr.Accordion("خلاصه چت", open=False):
173
- with gr.Row():
174
- self.summary_output = gr.Textbox(label="📌 خلاصه مکالمه", interactive=False)
175
  with gr.Accordion("تنظیمات پیشرفته", open=False):
 
 
 
 
 
176
  with gr.Row():
177
- self.model_dropdown = gr.Dropdown(label="🔍 انتخاب مدل", choices=models, value=default_model)
178
- self.tone_dropdown = gr.Dropdown(label="🎭 لحن پاسخ", choices=["رسمی", "محاوره‌ای", "علمی", "طنزآمیز"], value="رسمی")
179
- self.language_dropdown = gr.Dropdown(label="🌐 زبان چت بات", choices=["فارسی", "انگلیسی", "عربی"], value="فارسی")
180
- self.token_count = gr.Textbox(label="🔢 تعداد توکن‌ها", interactive=False)
181
- self.token_price = gr.Textbox(label="💰 هزینه تخمینی", interactive=False)
182
  with gr.Row():
183
- self.creativity_slider = gr.Slider(label="🎨 خلاقیت (Temperature)", minimum=0.0, maximum=1.0, step=0.1, value=0.7)
184
- self.response_length_dropdown = gr.Dropdown(label="📏 طول پاسخ", choices=["کوتاه", "بلند"], value="بلند")
185
- self.keywords_input = gr.Textbox(label="🔑 کلمات کلیدی (اختیاری)")
186
- self.welcome_message_input = gr.Textbox(label="👋 پیام خوش آمدگویی (اختیاری)")
187
- self.exclusion_words_input = gr.Textbox(label="🚫 کلمات استثنا (اختیاری)")
188
- self.del_button.click(
189
- self.clear_chat,
 
 
190
  inputs=[],
191
- outputs=[self.chatbot, self.summary_output, self.token_count, self.token_price]
192
  )
193
- self.submit_button.click(
194
- self.process_chat,
 
195
  inputs=[
196
- self.query_input, self.file_input, self.summarize_checkbox,
197
- self.tone_dropdown, self.model_dropdown, self.creativity_slider,
198
- self.keywords_input, self.language_dropdown, self.response_length_dropdown,
199
- self.welcome_message_input, self.exclusion_words_input
200
  ],
201
- outputs=[self.chatbot, self.summary_output, self.token_count, self.token_price]
202
  )
203
- self.query_input.submit(
204
- self.process_chat,
 
205
  inputs=[
206
- self.query_input, self.file_input, self.summarize_checkbox,
207
- self.tone_dropdown, self.model_dropdown, self.creativity_slider,
208
- self.keywords_input, self.language_dropdown, self.response_length_dropdown,
209
- self.welcome_message_input, self.exclusion_words_input
210
  ],
211
- outputs=[self.chatbot, self.summary_output, self.token_count, self.token_price]
212
  )
213
- def process_chat(self, query, file_obj, summarize, tone, model_name, creativity,
214
- keywords, language, response_length, welcome_message, exclusion_words):
215
- response, summary, total_tokens, price = self.ai.answer_query(
216
- query, file_obj, summarize, tone, model_name, creativity,
217
- keywords, language, response_length, welcome_message, exclusion_words
218
- )
219
- return self.ai.chat_history, summary, total_tokens, price
220
- def clear_chat(self):
221
- self.ai.clear_history()
222
- return self.ai.chat_history, "", 0, "0 دلار"
223
- def launch(self):
224
- self.interface.launch()
225
-
226
- if __name__ == "__main__":
227
- ai_core = AICore()
228
- chat_app = ChatInterface(ai_core)
229
- chat_app.launch()
 
1
  import os
2
  import re
 
 
3
  from pypdf import PdfReader
4
  import gradio as gr
5
+
6
  from langchain_groq import ChatGroq
7
  from langchain_huggingface import HuggingFaceEmbeddings
8
+ from langchain.vectorstores import Chroma
9
  from langchain_core.documents import Document
10
  from langchain_text_splitters import RecursiveCharacterTextSplitter
11
+ from langchain.utilities import DuckDuckGoSearchAPIWrapper
12
+ from langchain_community.tools import DuckDuckGoSearchRun , DuckDuckGoSearchResults
13
+
14
+ embeddings = HuggingFaceEmbeddings(model_name="heydariAI/persian-embeddings")
15
+ vector_store = Chroma(embedding_function=embeddings)
16
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
17
 
18
  models = ["deepseek-r1-distill-llama-70b", "llama-3.3-70b-versatile", "gemma2-9b-it"]
19
  default_model = models[0]
20
 
21
+ model_instance = ChatGroq(
22
+ api_key="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",
23
+ model_name=default_model
24
+ )
25
+
26
+ chat_history = []
27
+ PRICE_PER_TOKEN = 0.00001
28
+
29
+ def summarize_chat(model):
30
+ """Generate a short summary of the conversation so far."""
31
+ chat_text = "\n".join([f"پرسش: {q}\nپاسخ: {a}" for q, a in chat_history])
32
+ summary_prompt = f"یک خلاصه کوتاه از مکالمه زیر ارائه کن:\n\n{chat_text}\n\nخلاصه:"
33
+ summary_response = model.invoke(summary_prompt)
34
+ return summary_response.content
35
+
36
+ def process_file(file_path):
37
+ """Process the uploaded file (PDF or TXT) and index its content."""
38
+ if not file_path:
39
+ return None
40
+ file_extension = os.path.splitext(file_path)[1].lower()
41
+ try:
42
+ if file_extension == ".pdf":
43
+ reader = PdfReader(file_path)
44
+ file_text = "\n".join(page.extract_text() for page in reader.pages)
45
+ elif file_extension == ".txt":
46
+ with open(file_path, "r", encoding="utf-8") as f:
47
+ file_text = f.read()
48
+ else:
49
+ raise ValueError(f"Unsupported file format: {file_extension}")
50
+
51
+ file_docs = [Document(page_content=file_text, metadata={"source": "uploaded_file"})]
52
+ file_splits = text_splitter.split_documents(file_docs)
53
+ vector_store.add_documents(file_splits)
54
+ return file_text
55
+ except Exception as e:
56
+ raise RuntimeError(f"Error processing file: {str(e)}")
57
+
58
+ def answer_query(query, file_path, summarize, tone, model_name, creativity, keywords, language, response_length, welcome_message, exclusion_words):
59
+ """
60
+ Generate an answer by searching indexed content, applying prompt instructions,
61
+ and querying the language model.
62
+ """
63
+ global chat_history, model_instance
64
+
65
+ model_instance = ChatGroq(
66
+ api_key="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",
67
+ model_name=model_name
68
+ )
69
+ try:
70
+ if file_path:
71
+ process_file(file_path)
72
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  search_query = f"{keywords} {query}" if keywords else query
74
+ retrieved_docs = vector_store.similarity_search(search_query, k=3)
75
  knowledge = "\n\n".join(doc.page_content for doc in retrieved_docs)
76
+
77
  tone_prompts = {
78
  "رسمی": "پاسخ را با لحنی رسمی و مودبانه ارائه کن.",
79
  "محاوره‌ای": "پاسخ را به صورت دوستانه ارائه کن.",
 
81
  "طنزآمیز": "پاسخ را با لحنی طنزآمیز ارائه کن.",
82
  }
83
  tone_instruction = tone_prompts.get(tone, f"پاسخ را به زبان {language} ارائه کن.")
84
+ language_instruction = (f"پاسخ را فقط به زبان {language} ارائه کن و از زبان دیگری استفاده نکن مگر آنکه بخواهی کد بنویسی که در آن صورت فقط از زبان انگلیسی استفاده کن مگر اینکه کاربر از تو درخواست کند از زبان دیگری استفاده بکنی و از زبان چینی استفاده نکن.") if language else ""
 
85
  if response_length == "کوتاه":
86
  length_instruction = "پاسخ را به صورت مختصر ارائه کن."
87
  elif response_length == "بلند":
 
89
  else:
90
  length_instruction = ""
91
  exclusion_instruction = f"از کلمات زیر در پاسخ استفاده نکن: {exclusion_words}" if exclusion_words else ""
92
+
93
  prompt = (
94
+ f"شما ParvizGPT هستید، یک دستیار هوش مصنوعی ساخته شده توسط امیرمهدی پرویز دانشجو دانشگاه صنعتی کرمانشاه "
95
  f"{tone_instruction} {language_instruction} {length_instruction} {exclusion_instruction}\n\n"
96
  )
97
+
98
+ if welcome_message and not chat_history:
99
  prompt = f"{welcome_message}\n\n" + prompt
100
+
101
+ if chat_history:
102
+ conversation_history = "\n".join([f"پرسش: {q}\nپاسخ: {a}" for q, a in chat_history])
103
  prompt = f"{conversation_history}\n\n" + prompt
104
+
105
  prompt += f"اطلاعات مرتبط:\n{knowledge}\n\nسوال: {query}\nپاسخ:"
106
+
107
+ response = model_instance.invoke(prompt, temperature=creativity)
108
+ cleaned_response = remove_think_sections(response.content)
109
+
110
+ chat_history.append((query, cleaned_response))
111
+
112
+ total_tokens, price = calculate_price(prompt, cleaned_response)
113
+ summary = summarize_chat(model_instance) if summarize else "خلاصه‌سازی غیرفعال است."
114
+
115
+ return cleaned_response, summary, total_tokens, price
116
+ except Exception as e:
117
+ return f"خطا: {str(e)}", "", 0, "0 دلار"
118
+
119
+ def count_tokens(text):
120
+ """A simple token counter based on whitespace splitting."""
121
+ return len(text.split())
122
+
123
+ def calculate_price(input_text, output_text):
124
+ """Estimate the total token count and cost."""
125
+ input_tokens = count_tokens(input_text)
126
+ output_tokens = count_tokens(output_text)
127
+ total_tokens = input_tokens + output_tokens
128
+ total_price = total_tokens * PRICE_PER_TOKEN
129
+ return total_tokens, f"{total_price:.6f} دلار"
130
+
131
+ def remove_think_sections(response_text):
132
+ """Remove any <think>...</think> sections from the response text."""
133
+ return re.sub(r"<think>.*?</think>", "", response_text, flags=re.DOTALL)
134
+
135
+ def chat_with_bot(query, file, summarize, tone, model_name, creativity, keywords, language, response_length, welcome_message, exclusion_words, chat_history_input):
136
+ """
137
+ Handle a chat message by processing file (if provided), querying the model,
138
+ and updating the chat history.
139
+ """
140
+ file_path = file.name if file else None
141
+ response, summary, total_tokens, price = answer_query(
142
+ query, file_path, summarize, tone, model_name, creativity, keywords,
143
+ language, response_length, welcome_message, exclusion_words
144
+ )
145
+ chat_history_input.append((query, response))
146
+ return chat_history_input, summary, total_tokens, price
147
+
148
+ def clear_memory():
149
+ """Clear the conversation history."""
150
+ global chat_history
151
+ chat_history = []
152
+ return [], "", 0, "0 دلار"
153
+
154
+ def search_and_summarize(query: str) -> str:
155
+ """Perform web and news search, then summarize results using the model."""
156
+ try:
157
+ regular_search = DuckDuckGoSearchRun()
158
+ regular_results = regular_search.invoke(query)
159
+
160
+ news_search = DuckDuckGoSearchResults(backend="news")
161
+ news_results = news_search.invoke(query)
162
+
163
+ combined_results = f"نتایج جستجو:\n{regular_results}\n\nنتایج اخبار:\n{news_results}"
164
+
165
+ summary_prompt = (
166
+ "لطفاً نتایج جستجو و اخبار زیر را به صورت خلاصه و منسجم به زبان فارسی ارائه دهید. "
167
+ "خلاصه باید شامل نکات کلیدی از هر دو بخش باشد و به صورت واضح و مختصر نوشته شود.\n\n"
168
+ f"{combined_results}\n\n"
169
+ "خلاصه:"
170
+ )
171
+
172
+ # Initialize the model
173
+ model = ChatGroq(
174
+ api_key="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",
175
+ model_name=default_model
176
+ )
177
+
178
+ summary_response = model.invoke(summary_prompt)
179
+ return f"🔍 نتایج کامل:\n{combined_results}\n\n 📝 خلاصه نتایج:\n{remove_think_sections(summary_response.content)}"
180
+ except Exception as e:
181
+ return f"خطا در پرد��زش جستجو: {str(e)}"
182
+
183
+ with gr.Blocks() as demo:
184
+ gr.Markdown("# Parviz Mind")
185
+ gr.Markdown("زنده باد")
186
+
187
+ with gr.Tabs():
188
+
189
+ with gr.TabItem("Chatbot"):
190
+ gr.Markdown("## 🤖 Parviz GPT - چت بات هوش مصنوعی")
191
  gr.Markdown("**یک فایل (PDF یا TXT) آپلود کنید و سوال خود را بپرسید.**")
192
+
193
+ chatbot = gr.Chatbot(label="💬 تاریخچه چت")
194
+ query_input = gr.Textbox(label=" سوال خود را وارد کنید")
195
+ summarize_checkbox = gr.Checkbox(label="📌 خلاصه‌ساز را فعال کن")
196
+ submit_button = gr.Button("🚀 ارسال")
197
+ del_button = gr.Button("🗑 پاک کردن حافظه")
198
+ file_input = gr.File(label="📂 آپلود فایل", file_types=[".pdf", ".txt"])
199
+
 
200
  with gr.Accordion("تنظیمات پیشرفته", open=False):
201
+
202
+ summary_output = gr.Textbox(label="📌 خلاصه مکالمه", interactive=False)
203
+ token_count = gr.Textbox(label="🔢 تعداد توکن‌ها", interactive=False)
204
+ token_price = gr.Textbox(label="💰 هزینه تخمینی", interactive=False)
205
+
206
  with gr.Row():
207
+ model_dropdown = gr.Dropdown(label="🔍 انتخاب مدل", choices=models, value=default_model)
208
+ tone_dropdown = gr.Dropdown(label="🎭 لحن پاسخ", choices=["رسمی", "محاوره‌ای", "علمی", "طنزآمیز"], value="رسمی")
209
+ language_dropdown = gr.Dropdown(label="🌐 زبان چت بات", choices=["فارسی", "انگلیسی", "عربی"], value="فارسی")
210
+
 
211
  with gr.Row():
212
+ creativity_slider = gr.Slider(label="🎨 خلاقیت (Temperature)", minimum=0.0, maximum=1.0, step=0.1, value=0.7)
213
+ response_length_dropdown = gr.Dropdown(label="📏 طول پاسخ", choices=["کوتاه", "بلند"], value="بلند")
214
+
215
+ keywords_input = gr.Textbox(label="🔑 کلمات کلیدی (اختیاری)")
216
+ welcome_message_input = gr.Textbox(label="👋 پیام خوش آمدگویی (اختیاری)")
217
+ exclusion_words_input = gr.Textbox(label="🚫 کلمات استثنا (اختیاری)")
218
+
219
+ del_button.click(
220
+ clear_memory,
221
  inputs=[],
222
+ outputs=[chatbot, summary_output, token_count, token_price]
223
  )
224
+
225
+ submit_button.click(
226
+ chat_with_bot,
227
  inputs=[
228
+ query_input, file_input, summarize_checkbox, tone_dropdown, model_dropdown,
229
+ creativity_slider, keywords_input, language_dropdown, response_length_dropdown,
230
+ welcome_message_input, exclusion_words_input, chatbot
 
231
  ],
232
+ outputs=[chatbot, summary_output, token_count, token_price]
233
  )
234
+
235
+ query_input.submit(
236
+ chat_with_bot,
237
  inputs=[
238
+ query_input, file_input, summarize_checkbox, tone_dropdown, model_dropdown,
239
+ creativity_slider, keywords_input, language_dropdown, response_length_dropdown,
240
+ welcome_message_input, exclusion_words_input, chatbot
 
241
  ],
242
+ outputs=[chatbot, summary_output, token_count, token_price]
243
  )
244
+
245
+ with gr.TabItem("Web Search"):
246
+ gr.Markdown("## 🌐 Web Search")
247
+ gr.Markdown("سؤالات خود را تایپ کنید:")
248
+ search_query = gr.Textbox(label="عبارت جستجو", placeholder="متنی برای جستجو وارد کنید...")
249
+ search_button = gr.Button("جستجو")
250
+ web_results_output = gr.Textbox(label="نتایج جستجو و خلاصه", lines=10)
251
+ search_button.click(fn=search_and_summarize, inputs=search_query, outputs=web_results_output)
252
+ search_query.submit(fn=search_and_summarize, inputs=search_query, outputs=web_results_output)
253
+
254
+ demo.launch()