Update app.py
Browse files
app.py
CHANGED
|
@@ -2,98 +2,70 @@ import os
|
|
| 2 |
import time
|
| 3 |
import streamlit as st
|
| 4 |
from groq import Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# ----------------- تنظیمات صفحه -----------------
|
| 7 |
-
st.set_page_config(page_title="چتبات ارتش -
|
| 8 |
-
|
| 9 |
-
# استایل فارسی و بکگراند
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
margin: 0;
|
| 63 |
-
font-weight: bold;
|
| 64 |
-
}
|
| 65 |
-
.subtitle {
|
| 66 |
-
font-size: 18px;
|
| 67 |
-
color: #34495e;
|
| 68 |
-
margin-top: 8px;
|
| 69 |
-
}
|
| 70 |
-
@keyframes fadeIn {
|
| 71 |
-
from { opacity: 0; transform: translateY(10px); }
|
| 72 |
-
to { opacity: 1; transform: translateY(0); }
|
| 73 |
-
}
|
| 74 |
-
</style>
|
| 75 |
-
""", unsafe_allow_html=True)
|
| 76 |
-
|
| 77 |
-
# ----------------- لوگو و عنوان -----------------
|
| 78 |
-
col1, col2, col3 = st.columns([1, 1, 1])
|
| 79 |
-
with col2:
|
| 80 |
-
st.image("army.png", width=240)
|
| 81 |
-
|
| 82 |
-
st.markdown("""
|
| 83 |
-
<div class="header-text">
|
| 84 |
-
<h1>چتبات ارتش</h1>
|
| 85 |
-
<div class="subtitle">دستیار هوشمند میدان نبرد - Powered by Groq</div>
|
| 86 |
-
</div>
|
| 87 |
-
""", unsafe_allow_html=True)
|
| 88 |
-
|
| 89 |
-
# ----------------- اتصال به Groq -----------------
|
| 90 |
-
api_key = "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"
|
| 91 |
-
|
| 92 |
-
client = Groq(api_key=api_key)
|
| 93 |
-
|
| 94 |
-
selected_model = "llama3-70b-8192" # بهترین مدل Groq
|
| 95 |
-
|
| 96 |
-
# ----------------- استیت ذخیرهی پیامها -----------------
|
| 97 |
if 'messages' not in st.session_state:
|
| 98 |
st.session_state.messages = []
|
| 99 |
|
|
@@ -106,28 +78,23 @@ for msg in st.session_state.messages:
|
|
| 106 |
st.markdown(f"🗨️ {msg['content']}", unsafe_allow_html=True)
|
| 107 |
|
| 108 |
# ----------------- ورودی چت -----------------
|
| 109 |
-
prompt = st.chat_input("
|
| 110 |
|
| 111 |
if prompt:
|
| 112 |
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
| 113 |
st.session_state.pending_prompt = prompt
|
| 114 |
st.rerun()
|
| 115 |
|
| 116 |
-
# ----------------- پاسخ
|
| 117 |
if st.session_state.pending_prompt:
|
| 118 |
with st.chat_message('ai'):
|
| 119 |
thinking = st.empty()
|
| 120 |
-
thinking.markdown("🤖 در حال فکر
|
| 121 |
|
| 122 |
try:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
{"role": "user", "content": st.session_state.pending_prompt}
|
| 127 |
-
],
|
| 128 |
-
model=selected_model,
|
| 129 |
-
)
|
| 130 |
-
answer = chat_completion.choices[0].message.content.strip()
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
answer = f"خطا در پاسخدهی: {str(e)}"
|
|
|
|
| 2 |
import time
|
| 3 |
import streamlit as st
|
| 4 |
from groq import Groq
|
| 5 |
+
from langchain.document_loaders import PyPDFLoader
|
| 6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
+
from langchain.schema import Document as LangchainDocument
|
| 8 |
+
from langchain.vectorstores import FAISS
|
| 9 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 10 |
+
from langchain.chains import RetrievalQA
|
| 11 |
+
from langchain.llms import OpenAI
|
| 12 |
|
| 13 |
# ----------------- تنظیمات صفحه -----------------
|
| 14 |
+
st.set_page_config(page_title="چتبات ارتش - فقط از PDF", page_icon="🪖", layout="wide")
|
| 15 |
+
|
| 16 |
+
# استایل فارسی و بکگراند (مثل قبل...)
|
| 17 |
+
|
| 18 |
+
# ----------------- تعریف کلید API -----------------
|
| 19 |
+
groq_api_key = "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"
|
| 20 |
+
|
| 21 |
+
# ----------------- لود PDF و ساخت ایندکس -----------------
|
| 22 |
+
@st.cache_resource
|
| 23 |
+
def build_pdf_index():
|
| 24 |
+
with st.spinner('📄 در حال پردازش فایل PDF...'):
|
| 25 |
+
loader = PyPDFLoader("test1.pdf")
|
| 26 |
+
pages = loader.load()
|
| 27 |
+
|
| 28 |
+
# تکهتکه کردن متن PDF
|
| 29 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 30 |
+
chunk_size=500,
|
| 31 |
+
chunk_overlap=50
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
texts = []
|
| 35 |
+
for page in pages:
|
| 36 |
+
texts.extend(splitter.split_text(page.page_content))
|
| 37 |
+
|
| 38 |
+
# تبدیل به Document
|
| 39 |
+
documents = [LangchainDocument(page_content=t) for t in texts]
|
| 40 |
+
|
| 41 |
+
# استفاده از HuggingFaceEmbedding محلی برای FAISS
|
| 42 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 43 |
+
|
| 44 |
+
vectordb = FAISS.from_documents(documents, embedding=embeddings)
|
| 45 |
+
|
| 46 |
+
return vectordb
|
| 47 |
+
|
| 48 |
+
# ----------------- ساختن Index از PDF -----------------
|
| 49 |
+
index = build_pdf_index()
|
| 50 |
+
|
| 51 |
+
# ----------------- تعریف LLM Groq -----------------
|
| 52 |
+
client = Groq(api_key=groq_api_key)
|
| 53 |
+
|
| 54 |
+
class GroqLLM(OpenAI):
|
| 55 |
+
def __init__(self, api_key, model_name):
|
| 56 |
+
super().__init__(openai_api_key=api_key, model_name=model_name, base_url="https://api.groq.com/openai/v1")
|
| 57 |
+
|
| 58 |
+
llm = GroqLLM(api_key=groq_api_key, model_name="llama3-70b-8192")
|
| 59 |
+
|
| 60 |
+
# ----------------- Retrieval Chain -----------------
|
| 61 |
+
chain = RetrievalQA.from_chain_type(
|
| 62 |
+
llm=llm,
|
| 63 |
+
retriever=index.as_retriever(),
|
| 64 |
+
chain_type="stuff",
|
| 65 |
+
input_key="question"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# ----------------- استیت برای چت -----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if 'messages' not in st.session_state:
|
| 70 |
st.session_state.messages = []
|
| 71 |
|
|
|
|
| 78 |
st.markdown(f"🗨️ {msg['content']}", unsafe_allow_html=True)
|
| 79 |
|
| 80 |
# ----------------- ورودی چت -----------------
|
| 81 |
+
prompt = st.chat_input("سوالی در مورد فایل بپرس...")
|
| 82 |
|
| 83 |
if prompt:
|
| 84 |
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
| 85 |
st.session_state.pending_prompt = prompt
|
| 86 |
st.rerun()
|
| 87 |
|
| 88 |
+
# ----------------- پاسخ مدل فقط از روی PDF -----------------
|
| 89 |
if st.session_state.pending_prompt:
|
| 90 |
with st.chat_message('ai'):
|
| 91 |
thinking = st.empty()
|
| 92 |
+
thinking.markdown("🤖 در حال فکر کردن از روی PDF...")
|
| 93 |
|
| 94 |
try:
|
| 95 |
+
# گرفتن جواب فقط از PDF
|
| 96 |
+
response = chain.run(f"سوال: {st.session_state.pending_prompt}")
|
| 97 |
+
answer = response.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
answer = f"خطا در پاسخدهی: {str(e)}"
|