GIGAParviz commited on
Commit
a2275e1
·
verified ·
1 Parent(s): f1a01e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
app.py CHANGED
@@ -19,17 +19,13 @@ model = ChatGroq(api_key="gsk_kqPWbbWhDN2egNA4k8X3WGdyb3FYEaW2TzHfLhDQuzgMkTm9C7
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:
@@ -53,15 +49,6 @@ def process_file(file_path):
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
@@ -121,18 +108,34 @@ def answer_query(query, file_path, summarize, tone, model_name, creativity, keyw
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
- return [], "", 0, "0 تومان"
133
 
134
  with gr.Blocks() as demo:
135
- gr.Markdown("## 🤖 Parviz GPT")
136
  gr.Markdown("**یک فایل (PDF یا TXT) آپلود کنید و سوال خود را بپرسید.**")
137
 
138
  chatbot = gr.Chatbot(label="💬 تاریخچه چت")
 
19
  chat_history = []
20
  PRICE_PER_TOKEN = 0.00001
21
 
22
+ def summarize_chat(model):
23
 
24
+ chat_text = "\n".join([f"پرسش: {q}\nپاسخ: {a}" for q, a in chat_history])
25
+ summary_prompt = f"یک خلاصه کوتاه از مکالمه زیر ارائه کن:\n\n{chat_text}\n\nخلاصه:"
26
+ summary_response = model.invoke(summary_prompt)
27
+ return summary_response.content
28
+
 
 
 
 
 
29
  def process_file(file_path):
30
  """Process file and store in ChromaDB."""
31
  if not file_path:
 
49
  except Exception as e:
50
  raise RuntimeError(f"Error processing file: {str(e)}")
51
 
 
 
 
 
 
 
 
 
 
52
 
53
  def answer_query(query, file_path, summarize, tone, model_name, creativity, keywords, language, response_length, welcome_message, exclusion_words):
54
  global chat_history
 
108
  except Exception as e:
109
  return f"خطا: {str(e)}", "", 0, "0 دلار"
110
 
111
+ def count_tokens(text):
112
+ return len(text.split())
113
+
114
+ def calculate_price(input_text, output_text):
115
+ input_tokens = count_tokens(input_text)
116
+ output_tokens = count_tokens(output_text)
117
+ total_tokens = input_tokens + output_tokens
118
+ total_price = total_tokens * PRICE_PER_TOKEN
119
+ return total_tokens, f"{total_price:.6f} دلار"
120
+
121
+ def remove_think_sections(response_text):
122
+ return re.sub(r"<think>.*?</think>", "", response_text, flags=re.DOTALL)
123
+
124
+ def chat_with_bot(query, file, summarize, tone, model_name, creativity, keywords, language, response_length, welcome_message, exclusion_words, chat_history):
125
  file_path = file.name if file else None
126
+ response, summary, total_tokens, price = answer_query(query, file_path, summarize, tone, model_name, creativity, keywords, language, response_length, welcome_message, exclusion_words)
127
 
128
+ chat_history.append((query, response))
129
+
130
+ return chat_history, summary, total_tokens, price
131
 
132
  def clear_memory():
133
  global chat_history
134
  chat_history = []
135
+ return [], "", 0, "0 دلار"
136
 
137
  with gr.Blocks() as demo:
138
+ gr.Markdown("## 🤖 Parviz GPT - چت بات هوش مصنوعی")
139
  gr.Markdown("**یک فایل (PDF یا TXT) آپلود کنید و سوال خود را بپرسید.**")
140
 
141
  chatbot = gr.Chatbot(label="💬 تاریخچه چت")