Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,146 +1,187 @@
|
|
1 |
import os
|
2 |
import re
|
|
|
3 |
import gradio as gr
|
4 |
from langchain_groq import ChatGroq
|
5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
6 |
-
from
|
7 |
from langchain_core.documents import Document
|
8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
9 |
|
10 |
embeddings = HuggingFaceEmbeddings(model_name="heydariAI/persian-embeddings")
|
11 |
-
vector_store =
|
12 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
13 |
-
model = ChatGroq(api_key="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", model_name="deepseek-r1-distill-llama-70b")
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
PRICE_PER_TOKEN = 0.00001
|
18 |
|
19 |
def count_tokens(text):
|
20 |
-
"""تخمین تعداد توکنهای متن."""
|
21 |
return len(text.split())
|
22 |
|
23 |
def calculate_price(input_text, output_text):
|
24 |
-
"""محاسبه هزینه بر اساس تعداد توکنها."""
|
25 |
input_tokens = count_tokens(input_text)
|
26 |
output_tokens = count_tokens(output_text)
|
27 |
total_tokens = input_tokens + output_tokens
|
28 |
total_price = total_tokens * PRICE_PER_TOKEN
|
29 |
-
return total_tokens, f"{total_price:.6f}
|
30 |
|
31 |
def process_file(file_path):
|
32 |
-
"""
|
33 |
if not file_path:
|
34 |
return None
|
35 |
-
|
36 |
file_extension = os.path.splitext(file_path)[1].lower()
|
37 |
-
|
38 |
try:
|
39 |
if file_extension == ".pdf":
|
40 |
-
|
41 |
reader = PdfReader(file_path)
|
42 |
-
|
43 |
elif file_extension == ".txt":
|
44 |
with open(file_path, "r", encoding="utf-8") as f:
|
45 |
-
|
46 |
else:
|
47 |
-
raise ValueError(f"
|
48 |
-
except Exception as e:
|
49 |
-
raise RuntimeError(f"خطا در پردازش فایل: {str(e)}")
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def remove_think_sections(response_text):
|
53 |
-
"
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
|
58 |
-
def answer_query(query, file_path, summarize, tone):
|
59 |
-
"""پاسخ به سوالات کاربر با تنظیم لحن و محاسبه هزینه توکن."""
|
60 |
global chat_history
|
|
|
|
|
61 |
try:
|
62 |
-
file_content = process_file(file_path) if file_path else None
|
63 |
-
if file_content:
|
64 |
-
file_docs = [Document(page_content=file_content, metadata={"source": "uploaded_file"})]
|
65 |
-
file_splits = text_splitter.split_documents(file_docs)
|
66 |
-
vector_store.add_documents(file_splits)
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
knowledge = "\n\n".join(doc.page_content for doc in retrieved_docs)
|
70 |
|
71 |
tone_prompts = {
|
72 |
"رسمی": "پاسخ را با لحنی رسمی و مودبانه ارائه کن.",
|
73 |
-
"محاورهای": "پاسخ را به صورت دوستانه
|
74 |
-
"علمی": "پاسخ را با
|
75 |
-
"طنزآمیز": "پاسخ را با لحنی طنزآمیز
|
76 |
}
|
77 |
-
tone_instruction = tone_prompts.get(tone, "پاسخ را به زبان
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
prompt = (
|
80 |
-
f"شما ParvizGPT هستید، یک دستیار هوش مصنوعی
|
81 |
-
f"
|
82 |
-
f"\n\nاطلاعات مرتبط:\n{knowledge}\n\nسوال: {query}\nپاسخ:"
|
83 |
)
|
84 |
|
85 |
-
|
86 |
-
|
87 |
|
88 |
-
|
|
|
|
|
89 |
|
90 |
-
|
91 |
|
92 |
-
|
|
|
93 |
|
94 |
-
|
95 |
|
96 |
-
|
|
|
97 |
|
|
|
98 |
except Exception as e:
|
99 |
return f"خطا: {str(e)}", "", 0, "0 دلار"
|
100 |
|
101 |
-
def
|
102 |
-
"""خلاصهسازی مکالمات اخیر."""
|
103 |
-
chat_text = "\n".join([f"پرسش: {q}\nپاسخ: {a}" for q, a in chat_history])
|
104 |
-
summary_prompt = f"یک خلاصه کوتاه و دقیق از مکالمه زیر ارائه کن:\n\n{chat_text}\n\nخلاصه:"
|
105 |
-
summary_response = model.invoke(summary_prompt)
|
106 |
-
return summary_response.content
|
107 |
-
|
108 |
-
def chat_with_bot(query, file, summarize, tone):
|
109 |
-
"""رابط Gradio برای چت."""
|
110 |
file_path = file.name if file else None
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
with gr.Blocks() as demo:
|
115 |
-
gr.Markdown("## 🤖 Parviz GPT")
|
116 |
gr.Markdown("**یک فایل (PDF یا TXT) آپلود کنید و سوال خود را بپرسید.**")
|
117 |
-
|
118 |
-
with gr.Column():
|
119 |
|
120 |
-
|
121 |
-
summary_output = gr.Textbox(label="📌 خلاصه مکالمه", interactive=False)
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
with gr.Row():
|
126 |
-
summarize_checkbox = gr.Checkbox(label="📌 خلاصهساز را فعال کن")
|
127 |
-
submit_button = gr.Button("🚀 ارسال")
|
128 |
-
tone_dropdown = gr.Dropdown(label="🎭 انتخاب لحن پاسخ", choices=["رسمی", "محاورهای", "علمی", "طنزآمیز"], value="رسمی")
|
129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
with gr.Row():
|
132 |
-
|
133 |
-
|
|
|
134 |
|
135 |
with gr.Row():
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
demo.launch()
|
|
|
1 |
import os
|
2 |
import re
|
3 |
+
from pypdf import PdfReader
|
4 |
import gradio as gr
|
5 |
from langchain_groq import ChatGroq
|
6 |
from langchain_huggingface import HuggingFaceEmbeddings
|
7 |
+
from langchain.vectorstores import Chroma
|
8 |
from langchain_core.documents import Document
|
9 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
10 |
|
11 |
embeddings = HuggingFaceEmbeddings(model_name="heydariAI/persian-embeddings")
|
12 |
+
vector_store = Chroma(embedding_function=embeddings)
|
13 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
|
|
14 |
|
15 |
+
models = ["deepseek-r1-distill-llama-70b", "llama-3.3-70b-versatile", "gemma2-9b-it"]
|
16 |
+
default_model = models[0]
|
17 |
+
model = ChatGroq(api_key="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", model_name=default_model)
|
18 |
+
|
19 |
+
chat_history = []
|
20 |
+
PRICE_PER_TOKEN = 0.00001
|
21 |
|
|
|
22 |
|
23 |
def count_tokens(text):
|
|
|
24 |
return len(text.split())
|
25 |
|
26 |
def calculate_price(input_text, output_text):
|
|
|
27 |
input_tokens = count_tokens(input_text)
|
28 |
output_tokens = count_tokens(output_text)
|
29 |
total_tokens = input_tokens + output_tokens
|
30 |
total_price = total_tokens * PRICE_PER_TOKEN
|
31 |
+
return total_tokens, f"{total_price:.6f} هزار تومان"
|
32 |
|
33 |
def process_file(file_path):
|
34 |
+
"""Process file and store in ChromaDB."""
|
35 |
if not file_path:
|
36 |
return None
|
|
|
37 |
file_extension = os.path.splitext(file_path)[1].lower()
|
|
|
38 |
try:
|
39 |
if file_extension == ".pdf":
|
40 |
+
|
41 |
reader = PdfReader(file_path)
|
42 |
+
file_text = "\n".join(page.extract_text() for page in reader.pages)
|
43 |
elif file_extension == ".txt":
|
44 |
with open(file_path, "r", encoding="utf-8") as f:
|
45 |
+
file_text = f.read()
|
46 |
else:
|
47 |
+
raise ValueError(f"Unsupported file format: {file_extension}")
|
|
|
|
|
48 |
|
49 |
+
file_docs = [Document(page_content=file_text, metadata={"source": "uploaded_file"})]
|
50 |
+
file_splits = text_splitter.split_documents(file_docs)
|
51 |
+
vector_store.add_documents(file_splits)
|
52 |
+
return file_text
|
53 |
+
except Exception as e:
|
54 |
+
raise RuntimeError(f"Error processing file: {str(e)}")
|
55 |
|
56 |
def remove_think_sections(response_text):
|
57 |
+
return re.sub(r"<think>.*?</think>", "", response_text, flags=re.DOTALL)
|
58 |
+
|
59 |
+
def summarize_chat(model):
|
60 |
|
61 |
+
chat_text = "\n".join([f"پرسش: {q}\nپاسخ: {a}" for q, a in chat_history])
|
62 |
+
summary_prompt = f"یک خلاصه کوتاه از مکالمه زیر ارائه کن:\n\n{chat_text}\n\nخلاصه:"
|
63 |
+
summary_response = model.invoke(summary_prompt)
|
64 |
+
return summary_response.content
|
65 |
|
66 |
+
def answer_query(query, file_path, summarize, tone, model_name, creativity, keywords, language, response_length, welcome_message, exclusion_words):
|
|
|
67 |
global chat_history
|
68 |
+
|
69 |
+
model = ChatGroq(api_key="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", model_name=model_name)
|
70 |
try:
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
if file_path:
|
73 |
+
process_file(file_path)
|
74 |
+
|
75 |
+
search_query = f"{keywords} {query}" if keywords else query
|
76 |
+
retrieved_docs = vector_store.similarity_search(search_query, k=3)
|
77 |
knowledge = "\n\n".join(doc.page_content for doc in retrieved_docs)
|
78 |
|
79 |
tone_prompts = {
|
80 |
"رسمی": "پاسخ را با لحنی رسمی و مودبانه ارائه کن.",
|
81 |
+
"محاورهای": "پاسخ را به صورت دوستانه ارائه کن.",
|
82 |
+
"علمی": "پاسخ را با استدلالهای منطقی ارائه کن.",
|
83 |
+
"طنزآمیز": "پاسخ را با لحنی طنزآمیز ارائه کن.",
|
84 |
}
|
85 |
+
tone_instruction = tone_prompts.get(tone, (f"پاسخ را به زبان {language} ارائه کن."))
|
86 |
+
|
87 |
+
language_instruction = f"پاسخ را فقط به زبان {language} ارائه کن و از زبان دیگری استفاده نکن مگر آنکه بخواهی کد بنویسی که در آن صورت فقط از زبان انگلیسی استفاده کن مگر اینکه کاربر از تو درخواست کند از زبان دیگری استفاده بکنی و از زبان چینی استفاده نکن." if language else ""
|
88 |
+
|
89 |
+
if response_length == "کوتاه":
|
90 |
+
length_instruction = "پاسخ را به صورت مختصر ارائه کن."
|
91 |
+
elif response_length == "بلند":
|
92 |
+
length_instruction = "پاسخ را به صورت مفصل و جامع ارائه کن."
|
93 |
+
else:
|
94 |
+
length_instruction = ""
|
95 |
+
|
96 |
+
exclusion_instruction = f"از کلمات زیر در پاسخ استفاده نکن: {exclusion_words}" if exclusion_words else ""
|
97 |
|
98 |
prompt = (
|
99 |
+
f"شما ParvizGPT هستید، یک دستیار هوش مصنوعی ساخته شده توسط امیرمهدی پرویز دانشجو دانشگاه صنعتی کرمانشاه "
|
100 |
+
f"{tone_instruction} {language_instruction} {length_instruction} {exclusion_instruction}\n\n"
|
|
|
101 |
)
|
102 |
|
103 |
+
if welcome_message and not chat_history:
|
104 |
+
prompt = f"{welcome_message}\n\n" + prompt
|
105 |
|
106 |
+
if chat_history:
|
107 |
+
conversation_history = "\n".join([f"پرسش: {q}\nپاسخ: {a}" for q, a in chat_history])
|
108 |
+
prompt = f"{conversation_history}\n\n" + prompt
|
109 |
|
110 |
+
prompt += f"اطلاعات مرتبط:\n{knowledge}\n\nسوال: {query}\nپاسخ:"
|
111 |
|
112 |
+
response = model.invoke(prompt, temperature=creativity)
|
113 |
+
cleaned_response = remove_think_sections(response.content)
|
114 |
|
115 |
+
chat_history.append((query, cleaned_response))
|
116 |
|
117 |
+
total_tokens, price = calculate_price(prompt, cleaned_response)
|
118 |
+
summary = summarize_chat(model) if summarize else "خلاصهسازی غیرفعال است."
|
119 |
|
120 |
+
return cleaned_response, summary, total_tokens, price
|
121 |
except Exception as e:
|
122 |
return f"خطا: {str(e)}", "", 0, "0 دلار"
|
123 |
|
124 |
+
def chat_with_bot(query, file, summarize, tone, model_name, creativity, keywords, language, response_length, welcome_message, exclusion_words):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
file_path = file.name if file else None
|
126 |
+
return answer_query(query, file_path, summarize, tone, model_name, creativity, keywords, language, response_length, welcome_message, exclusion_words)
|
127 |
+
|
128 |
+
|
129 |
+
def clear_memory():
|
130 |
+
global chat_history
|
131 |
+
chat_history = []
|
132 |
+
|
133 |
+
return '' , '' , 0 , 0
|
134 |
|
135 |
with gr.Blocks() as demo:
|
136 |
+
gr.Markdown("## 🤖 Parviz GPT - چت بات هوش مصنوعی")
|
137 |
gr.Markdown("**یک فایل (PDF یا TXT) آپلود کنید و سوال خود را بپرسید.**")
|
|
|
|
|
138 |
|
139 |
+
chat_output = gr.Textbox(label="📝 پاسخ", interactive=False, lines=10)
|
|
|
140 |
|
141 |
+
query_input = gr.Textbox(label="❓ سوال خود را وارد کنید")
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
+
submit_button = gr.Button("🚀 ارسال")
|
144 |
+
del_button = gr.Button("پاک کردن حافظه")
|
145 |
+
summary_output = gr.Textbox(label="📌 خلاصه مکالمه", interactive=False)
|
146 |
+
token_count = gr.Textbox(label="🔢 تعداد توکنها", interactive=False)
|
147 |
+
token_price = gr.Textbox(label="💰 هزینه تخمینی", interactive=False)
|
148 |
+
file_input = gr.File(label="📂 آپلود فایل", file_types=[".pdf", ".txt"])
|
149 |
|
150 |
with gr.Row():
|
151 |
+
model_dropdown = gr.Dropdown(label="🔍 انتخاب مدل", choices=models, value=default_model)
|
152 |
+
tone_dropdown = gr.Dropdown(label="🎭 لحن پاسخ", choices=["رسمی", "محاورهای", "علمی", "طنزآمیز"], value="رسمی")
|
153 |
+
language_dropdown = gr.Dropdown(label="🌐 زبان چت بات", choices=["فارسی", "انگلیسی", "عربی"], value="فارسی")
|
154 |
|
155 |
with gr.Row():
|
156 |
+
creativity_slider = gr.Slider(label="🎨 خلاقیت (Temperature)", minimum=0.0, maximum=1.0, step=0.1, value=0.7)
|
157 |
+
response_length_dropdown = gr.Dropdown(label="📏 طول پاسخ", choices=["کوتاه", "بلند"], value="بلند")
|
158 |
+
|
159 |
+
keywords_input = gr.Textbox(label="🔑 کلمات کلیدی (اختیاری)")
|
160 |
+
welcome_message_input = gr.Textbox(label="👋 پیام خوش آمدگویی (اختیاری)")
|
161 |
+
exclusion_words_input = gr.Textbox(label="🚫 کلمات استثنا (اختیاری)")
|
162 |
+
summarize_checkbox = gr.Checkbox(label="📌 خلاصهساز را فعال کن")
|
163 |
+
|
164 |
+
del_button.click(clear_memory,
|
165 |
+
inputs=[],
|
166 |
+
outputs=[chat_output, summary_output, token_count, token_price])
|
167 |
+
|
168 |
+
|
169 |
+
query_input.submit(fn=chat_with_bot,
|
170 |
+
inputs=[query_input, file_input, summarize_checkbox, tone_dropdown, model_dropdown,
|
171 |
+
creativity_slider, keywords_input, language_dropdown, response_length_dropdown,
|
172 |
+
welcome_message_input, exclusion_words_input
|
173 |
+
],
|
174 |
+
outputs=[chat_output, summary_output, token_count, token_price])
|
175 |
+
|
176 |
+
|
177 |
+
submit_button.click(
|
178 |
+
chat_with_bot,
|
179 |
+
inputs=[query_input, file_input, summarize_checkbox, tone_dropdown, model_dropdown,
|
180 |
+
creativity_slider, keywords_input, language_dropdown, response_length_dropdown,
|
181 |
+
welcome_message_input, exclusion_words_input
|
182 |
+
],
|
183 |
+
outputs=[chat_output, summary_output, token_count, token_price]
|
184 |
+
)
|
185 |
+
|
186 |
|
187 |
demo.launch()
|