import os
import time
import streamlit as st
from groq import Groq
# ----------------- تنظیمات صفحه -----------------
st.set_page_config(page_title="چتبات ارتش - Powered by Groq", page_icon="🪖", layout="wide")
# استایل فارسی و بکگراند
st.markdown("""
""", unsafe_allow_html=True)
# ----------------- لوگو و عنوان -----------------
col1, col2, col3 = st.columns([1, 1, 1])
with col2:
st.image("army.png", width=240)
st.markdown("""
""", unsafe_allow_html=True)
# ----------------- اتصال به Groq -----------------
api_key = "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"
client = Groq(api_key=api_key)
selected_model = "llama3-70b-8192" # بهترین مدل Groq
# ----------------- استیت ذخیرهی پیامها -----------------
if 'messages' not in st.session_state:
st.session_state.messages = []
if 'pending_prompt' not in st.session_state:
st.session_state.pending_prompt = None
# ----------------- نمایش پیامهای قبلی -----------------
for msg in st.session_state.messages:
with st.chat_message(msg['role']):
st.markdown(f"🗨️ {msg['content']}", unsafe_allow_html=True)
# ----------------- ورودی چت -----------------
prompt = st.chat_input("چطور میتونم کمک کنم؟")
if prompt:
st.session_state.messages.append({'role': 'user', 'content': prompt})
st.session_state.pending_prompt = prompt
st.rerun()
# ----------------- پاسخ دادن مدل -----------------
if st.session_state.pending_prompt:
with st.chat_message('ai'):
thinking = st.empty()
thinking.markdown("🤖 در حال فکر کردن...")
try:
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": "پاسخ را همیشه رسمی و فارسی بده."},
{"role": "user", "content": st.session_state.pending_prompt}
],
model=selected_model,
)
answer = chat_completion.choices[0].message.content.strip()
except Exception as e:
answer = f"خطا در پاسخدهی: {str(e)}"
thinking.empty()
# انیمیشن تایپ پاسخ
full_response = ""
placeholder = st.empty()
for word in answer.split():
full_response += word + " "
placeholder.markdown(full_response + "▌")
time.sleep(0.03)
placeholder.markdown(full_response)
st.session_state.messages.append({'role': 'ai', 'content': full_response})
st.session_state.pending_prompt = None