Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Get token from Space secrets
|
6 |
+
API_TOKEN = os.getenv("HF_TOKEN_Mistral")
|
7 |
+
client = InferenceClient(token=API_TOKEN)
|
8 |
+
|
9 |
+
# Initialize session state
|
10 |
+
if "chat_history" not in st.session_state:
|
11 |
+
st.session_state.chat_history = []
|
12 |
+
if "corrected_sentence" not in st.session_state:
|
13 |
+
st.session_state.corrected_sentence = ""
|
14 |
+
|
15 |
+
# Title of the app in Arabic
|
16 |
+
st.title("مُحسِّن الجمل والدردشة")
|
17 |
+
|
18 |
+
# --- Sentence Correction Section ---
|
19 |
+
st.subheader("تحسين الجملة")
|
20 |
+
user_input = st.text_input("أدخل جملة لتحسينها:", "ذهبتو إلى الحديقه و العب.")
|
21 |
+
|
22 |
+
if st.button("تحسين الجملة"):
|
23 |
+
if user_input:
|
24 |
+
prompt = f"صحح وحسّن هذه الجملة باللغة العربية: '{user_input}'"
|
25 |
+
try:
|
26 |
+
response = client.text_generation(
|
27 |
+
prompt,
|
28 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
29 |
+
max_new_tokens=100,
|
30 |
+
temperature=0.7,
|
31 |
+
)
|
32 |
+
st.session_state.corrected_sentence = response.strip()
|
33 |
+
st.success(f"الجملة المُحسّنة: {st.session_state.corrected_sentence}")
|
34 |
+
except Exception as e:
|
35 |
+
st.error(f"خطأ: {str(e)}")
|
36 |
+
else:
|
37 |
+
st.warning("من فضلك أدخل جملة أولاً!")
|
38 |
+
|
39 |
+
# --- Chat Section ---
|
40 |
+
st.subheader("الدردشة حول الجملة المُحسّنة")
|
41 |
+
|
42 |
+
if st.session_state.corrected_sentence:
|
43 |
+
# Display chat history
|
44 |
+
for speaker, message in st.session_state.chat_history:
|
45 |
+
st.write(f"**{speaker}:** {message}")
|
46 |
+
|
47 |
+
# Define the chat submission function
|
48 |
+
def submit_chat():
|
49 |
+
chat_text = st.session_state.chat_input
|
50 |
+
if chat_text:
|
51 |
+
prompt = (
|
52 |
+
f"الجملة المُحسّنة هي: '{st.session_state.corrected_sentence}'. "
|
53 |
+
f"يسأل المستخدم: '{chat_text}'. أجب بشكل طبيعي وباللغة العربية."
|
54 |
+
)
|
55 |
+
try:
|
56 |
+
response = client.text_generation(
|
57 |
+
prompt,
|
58 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
59 |
+
max_new_tokens=150,
|
60 |
+
temperature=0.7,
|
61 |
+
)
|
62 |
+
st.session_state.chat_history.append(("أنت", chat_text))
|
63 |
+
st.session_state.chat_history.append(("النموذج", response.strip()))
|
64 |
+
st.session_state.chat_input = ""
|
65 |
+
except Exception as e:
|
66 |
+
st.error(f"حدث خطأ في الدردشة: {str(e)}")
|
67 |
+
|
68 |
+
# Chat input field
|
69 |
+
st.text_input(
|
70 |
+
"اسأل شيئاً حول الجملة المُحسّنة (اضغط Enter للإرسال):",
|
71 |
+
key="chat_input",
|
72 |
+
on_change=submit_chat,
|
73 |
+
)
|
74 |
+
|
75 |
+
else:
|
76 |
+
st.write("يرجى تحسين جملة أولاً لبدء الدردشة!")
|