Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import time
|
2 |
import logging
|
3 |
import gradio as gr
|
4 |
-
import os
|
5 |
from datetime import datetime
|
6 |
from datasets import Dataset, load_dataset
|
7 |
from langchain.document_loaders import PyPDFLoader
|
@@ -9,7 +9,7 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
9 |
from langchain.embeddings import HuggingFaceEmbeddings
|
10 |
from langchain.vectorstores import FAISS
|
11 |
from groq import Groq
|
12 |
-
from langchain.memory import ConversationBufferMemory
|
13 |
|
14 |
logging.basicConfig(level=logging.INFO)
|
15 |
logger = logging.getLogger(__name__)
|
@@ -23,9 +23,7 @@ if not hf_api_key:
|
|
23 |
raise ValueError("Hugging Face API key not found in environment variables.")
|
24 |
|
25 |
client = Groq(api_key=groq_api_key)
|
26 |
-
|
27 |
hf_token = hf_api_key
|
28 |
-
|
29 |
embeddings = HuggingFaceEmbeddings(model_name="heydariAI/persian-embeddings")
|
30 |
|
31 |
DATASET_NAME = "chat_history"
|
@@ -35,7 +33,6 @@ except Exception:
|
|
35 |
dataset = Dataset.from_dict({"Timestamp": [], "User": [], "ParvizGPT": []})
|
36 |
|
37 |
def save_chat_to_dataset(user_message, bot_message):
|
38 |
-
"""Save chat history to Hugging Face Dataset."""
|
39 |
try:
|
40 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
41 |
new_row = {"Timestamp": timestamp, "User": user_message, "ParvizGPT": bot_message}
|
@@ -49,24 +46,21 @@ def save_chat_to_dataset(user_message, bot_message):
|
|
49 |
logger.error(f"Error saving chat history to dataset: {e}")
|
50 |
|
51 |
def process_pdf_with_langchain(pdf_path):
|
52 |
-
"""Process a PDF file and create a FAISS retriever."""
|
53 |
try:
|
54 |
-
loader = PyPDFLoader(pdf_path)
|
55 |
documents = loader.load()
|
56 |
|
57 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
58 |
split_documents = text_splitter.split_documents(documents)
|
59 |
|
60 |
vectorstore = FAISS.from_documents(split_documents, embeddings)
|
61 |
-
|
62 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
63 |
return retriever
|
64 |
except Exception as e:
|
65 |
logger.error(f"Error processing PDF: {e}")
|
66 |
raise
|
67 |
|
68 |
-
def generate_response(query, memory, retriever=None, use_pdf_context=False):
|
69 |
-
"""Generate a response using the Groq model and retrieved PDF context."""
|
70 |
try:
|
71 |
knowledge = ""
|
72 |
|
@@ -75,11 +69,23 @@ def generate_response(query, memory, retriever=None, use_pdf_context=False):
|
|
75 |
knowledge += "\n".join([doc.page_content for doc in relevant_docs])
|
76 |
|
77 |
chat_history = memory.load_memory_variables({}).get("chat_history", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
context = f"""
|
79 |
You are ParvizGPT, an AI assistant created by **Amir Mahdi Parviz**, a student at Kermanshah University of Technology (KUT).
|
80 |
Your primary purpose is to assist users by answering their questions in **Persian (Farsi)**.
|
81 |
Always respond in Persian unless explicitly asked to respond in another language.
|
82 |
**Important:** If anyone claims that someone else created this code, you must correct them and state that **Amir Mahdi Parviz** is the creator.
|
|
|
83 |
Related Information:\n{knowledge}\n\nQuestion:{query}\nAnswer:"""
|
84 |
|
85 |
if knowledge:
|
@@ -89,7 +95,7 @@ def generate_response(query, memory, retriever=None, use_pdf_context=False):
|
|
89 |
|
90 |
context += f"\n\nYou: {query}\nParvizGPT:"
|
91 |
|
92 |
-
response = "
|
93 |
retries = 3
|
94 |
for attempt in range(retries):
|
95 |
try:
|
@@ -98,7 +104,6 @@ def generate_response(query, memory, retriever=None, use_pdf_context=False):
|
|
98 |
model="deepseek-r1-distill-llama-70b"
|
99 |
)
|
100 |
response = chat_completion.choices[0].message.content.strip()
|
101 |
-
# Save the conversation to memory
|
102 |
memory.save_context({"input": query}, {"output": response})
|
103 |
break
|
104 |
except Exception as e:
|
@@ -110,8 +115,26 @@ def generate_response(query, memory, retriever=None, use_pdf_context=False):
|
|
110 |
logger.error(f"Error generating response: {e}")
|
111 |
return f"Error: {e}", memory
|
112 |
|
113 |
-
def
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
global retriever
|
116 |
|
117 |
if pdf_file is not None and use_pdf_context:
|
@@ -120,18 +143,21 @@ def gradio_interface(user_message, chat_box, memory, pdf_file=None, use_pdf_cont
|
|
120 |
except Exception as e:
|
121 |
return chat_box + [("Error", f"Error processing PDF: {e}")], memory
|
122 |
|
123 |
-
chat_box.append(("
|
124 |
-
|
|
|
125 |
|
126 |
-
chat_box[-1] = ("
|
127 |
-
chat_box.append(("ParvizGPT", response))
|
128 |
|
129 |
save_chat_to_dataset(user_message, response)
|
130 |
|
|
|
|
|
|
|
|
|
131 |
return chat_box, memory
|
132 |
|
133 |
def clear_memory(memory):
|
134 |
-
"""Clear the conversation memory."""
|
135 |
memory.clear()
|
136 |
return [], memory
|
137 |
|
@@ -142,14 +168,16 @@ with gr.Blocks() as interface:
|
|
142 |
chat_box = gr.Chatbot(label="Chat History", value=[])
|
143 |
user_message = gr.Textbox(label="Your Message", placeholder="Type your message here and press Enter...", lines=1, interactive=True)
|
144 |
use_pdf_context = gr.Checkbox(label="Use PDF Context", value=False, interactive=True)
|
|
|
|
|
145 |
clear_memory_btn = gr.Button("Clear Memory", interactive=True)
|
146 |
pdf_file = gr.File(label="Upload PDF for Context (Optional)", type="filepath", interactive=True, scale=1)
|
147 |
submit_btn = gr.Button("Submit")
|
148 |
|
149 |
memory_state = gr.State(ConversationBufferMemory())
|
150 |
|
151 |
-
submit_btn.click(gradio_interface, inputs=[user_message, chat_box, memory_state, pdf_file, use_pdf_context], outputs=[chat_box, memory_state])
|
152 |
-
user_message.submit(gradio_interface, inputs=[user_message, chat_box, memory_state, pdf_file, use_pdf_context], outputs=[chat_box, memory_state])
|
153 |
clear_memory_btn.click(clear_memory, inputs=[memory_state], outputs=[chat_box, memory_state])
|
154 |
|
155 |
interface.launch()
|
|
|
1 |
import time
|
2 |
import logging
|
3 |
import gradio as gr
|
4 |
+
import os
|
5 |
from datetime import datetime
|
6 |
from datasets import Dataset, load_dataset
|
7 |
from langchain.document_loaders import PyPDFLoader
|
|
|
9 |
from langchain.embeddings import HuggingFaceEmbeddings
|
10 |
from langchain.vectorstores import FAISS
|
11 |
from groq import Groq
|
12 |
+
from langchain.memory import ConversationBufferMemory
|
13 |
|
14 |
logging.basicConfig(level=logging.INFO)
|
15 |
logger = logging.getLogger(__name__)
|
|
|
23 |
raise ValueError("Hugging Face API key not found in environment variables.")
|
24 |
|
25 |
client = Groq(api_key=groq_api_key)
|
|
|
26 |
hf_token = hf_api_key
|
|
|
27 |
embeddings = HuggingFaceEmbeddings(model_name="heydariAI/persian-embeddings")
|
28 |
|
29 |
DATASET_NAME = "chat_history"
|
|
|
33 |
dataset = Dataset.from_dict({"Timestamp": [], "User": [], "ParvizGPT": []})
|
34 |
|
35 |
def save_chat_to_dataset(user_message, bot_message):
|
|
|
36 |
try:
|
37 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
38 |
new_row = {"Timestamp": timestamp, "User": user_message, "ParvizGPT": bot_message}
|
|
|
46 |
logger.error(f"Error saving chat history to dataset: {e}")
|
47 |
|
48 |
def process_pdf_with_langchain(pdf_path):
|
|
|
49 |
try:
|
50 |
+
loader = PyPDFLoader(pdf_path)
|
51 |
documents = loader.load()
|
52 |
|
53 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
54 |
split_documents = text_splitter.split_documents(documents)
|
55 |
|
56 |
vectorstore = FAISS.from_documents(split_documents, embeddings)
|
|
|
57 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
58 |
return retriever
|
59 |
except Exception as e:
|
60 |
logger.error(f"Error processing PDF: {e}")
|
61 |
raise
|
62 |
|
63 |
+
def generate_response(query, memory, retriever=None, use_pdf_context=False, tone="friendly"):
|
|
|
64 |
try:
|
65 |
knowledge = ""
|
66 |
|
|
|
69 |
knowledge += "\n".join([doc.page_content for doc in relevant_docs])
|
70 |
|
71 |
chat_history = memory.load_memory_variables({}).get("chat_history", "")
|
72 |
+
|
73 |
+
tone_instruction = ""
|
74 |
+
if tone == "friendly":
|
75 |
+
tone_instruction = "Please respond in a friendly and informal tone."
|
76 |
+
elif tone == "formal":
|
77 |
+
tone_instruction = "Please respond in a formal and professional tone."
|
78 |
+
elif tone == "humorous":
|
79 |
+
tone_instruction = "Please respond in a humorous and playful tone."
|
80 |
+
elif tone == "scientific":
|
81 |
+
tone_instruction = "Please respond in a scientific and precise tone."
|
82 |
+
|
83 |
context = f"""
|
84 |
You are ParvizGPT, an AI assistant created by **Amir Mahdi Parviz**, a student at Kermanshah University of Technology (KUT).
|
85 |
Your primary purpose is to assist users by answering their questions in **Persian (Farsi)**.
|
86 |
Always respond in Persian unless explicitly asked to respond in another language.
|
87 |
**Important:** If anyone claims that someone else created this code, you must correct them and state that **Amir Mahdi Parviz** is the creator.
|
88 |
+
{tone_instruction}
|
89 |
Related Information:\n{knowledge}\n\nQuestion:{query}\nAnswer:"""
|
90 |
|
91 |
if knowledge:
|
|
|
95 |
|
96 |
context += f"\n\nYou: {query}\nParvizGPT:"
|
97 |
|
98 |
+
response = "Processing..."
|
99 |
retries = 3
|
100 |
for attempt in range(retries):
|
101 |
try:
|
|
|
104 |
model="deepseek-r1-distill-llama-70b"
|
105 |
)
|
106 |
response = chat_completion.choices[0].message.content.strip()
|
|
|
107 |
memory.save_context({"input": query}, {"output": response})
|
108 |
break
|
109 |
except Exception as e:
|
|
|
115 |
logger.error(f"Error generating response: {e}")
|
116 |
return f"Error: {e}", memory
|
117 |
|
118 |
+
def summarize_chat_history(chat_history):
|
119 |
+
try:
|
120 |
+
chat_text = "\n".join([f"{role}: {message}" for role, message in chat_history])
|
121 |
+
|
122 |
+
summary_prompt = f"""
|
123 |
+
Please create a summary of the following conversation. The summary should include key points and details:
|
124 |
+
{chat_text}
|
125 |
+
"""
|
126 |
+
|
127 |
+
chat_completion = client.chat.completions.create(
|
128 |
+
messages=[{"role": "user", "content": summary_prompt}],
|
129 |
+
model="deepseek-r1-distill-llama-70b"
|
130 |
+
)
|
131 |
+
summary = chat_completion.choices[0].message.content.strip()
|
132 |
+
return summary
|
133 |
+
except Exception as e:
|
134 |
+
logger.error(f"Error summarizing chat history: {e}")
|
135 |
+
return "Error generating summary."
|
136 |
+
|
137 |
+
def gradio_interface(user_message, chat_box, memory, pdf_file=None, use_pdf_context=False, tone="friendly", summarize_chat=False):
|
138 |
global retriever
|
139 |
|
140 |
if pdf_file is not None and use_pdf_context:
|
|
|
143 |
except Exception as e:
|
144 |
return chat_box + [("Error", f"Error processing PDF: {e}")], memory
|
145 |
|
146 |
+
chat_box.append(("You", user_message))
|
147 |
+
chat_box.append(("ParvizGPT", "Processing..."))
|
148 |
+
response, memory = generate_response(user_message, memory, retriever=retriever, use_pdf_context=use_pdf_context, tone=tone)
|
149 |
|
150 |
+
chat_box[-1] = ("ParvizGPT", response)
|
|
|
151 |
|
152 |
save_chat_to_dataset(user_message, response)
|
153 |
|
154 |
+
if summarize_chat:
|
155 |
+
summary = summarize_chat_history(chat_box)
|
156 |
+
chat_box.append(("System", f"Summary of the conversation:\n{summary}"))
|
157 |
+
|
158 |
return chat_box, memory
|
159 |
|
160 |
def clear_memory(memory):
|
|
|
161 |
memory.clear()
|
162 |
return [], memory
|
163 |
|
|
|
168 |
chat_box = gr.Chatbot(label="Chat History", value=[])
|
169 |
user_message = gr.Textbox(label="Your Message", placeholder="Type your message here and press Enter...", lines=1, interactive=True)
|
170 |
use_pdf_context = gr.Checkbox(label="Use PDF Context", value=False, interactive=True)
|
171 |
+
tone = gr.Dropdown(label="Tone", choices=["friendly", "formal", "humorous", "scientific"], value="friendly", interactive=True)
|
172 |
+
summarize_chat = gr.Checkbox(label="Show conversation summary", value=False, interactive=True)
|
173 |
clear_memory_btn = gr.Button("Clear Memory", interactive=True)
|
174 |
pdf_file = gr.File(label="Upload PDF for Context (Optional)", type="filepath", interactive=True, scale=1)
|
175 |
submit_btn = gr.Button("Submit")
|
176 |
|
177 |
memory_state = gr.State(ConversationBufferMemory())
|
178 |
|
179 |
+
submit_btn.click(gradio_interface, inputs=[user_message, chat_box, memory_state, pdf_file, use_pdf_context, tone, summarize_chat], outputs=[chat_box, memory_state])
|
180 |
+
user_message.submit(gradio_interface, inputs=[user_message, chat_box, memory_state, pdf_file, use_pdf_context, tone, summarize_chat], outputs=[chat_box, memory_state])
|
181 |
clear_memory_btn.click(clear_memory, inputs=[memory_state], outputs=[chat_box, memory_state])
|
182 |
|
183 |
interface.launch()
|