Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,97 +1,50 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
import os
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
st.session_state.chat_history = []
|
12 |
-
if "corrected_sentence" not in st.session_state:
|
13 |
-
st.session_state.corrected_sentence = ""
|
14 |
|
15 |
-
#
|
16 |
-
st.title("Sentence Improver & Chat
|
17 |
-
|
18 |
-
# --- Sentence Correction Section ---
|
19 |
-
st.subheader("Improve a Sentence")
|
20 |
-
user_input = st.text_input("Enter a sentence to improve:", "I goed to the park and play.")
|
21 |
|
|
|
|
|
|
|
22 |
if st.button("Improve Sentence"):
|
23 |
if user_input:
|
24 |
-
|
|
|
25 |
try:
|
26 |
-
response =
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
)
|
32 |
-
st.session_state.corrected_sentence = response.strip()
|
33 |
-
st.success(f"Improved Sentence: {st.session_state.corrected_sentence}")
|
34 |
except Exception as e:
|
35 |
st.error(f"Error: {str(e)}")
|
36 |
else:
|
37 |
st.warning("Please enter a sentence first!")
|
38 |
|
39 |
-
#
|
40 |
st.subheader("Chat About the Corrected Sentence")
|
41 |
-
if st.session_state
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
st.markdown(
|
48 |
-
f"<div style='text-align: right; margin: 5px;'><span style='background-color: #DCF8C6; padding: 8px; border-radius: 10px;'>{message}</span></div>",
|
49 |
-
unsafe_allow_html=True
|
50 |
-
)
|
51 |
-
else:
|
52 |
-
st.markdown(
|
53 |
-
f"<div style='text-align: left; margin: 5px;'><span style='background-color: #ECECEC; padding: 8px; border-radius: 10px;'>{message}</span></div>",
|
54 |
-
unsafe_allow_html=True
|
55 |
-
)
|
56 |
-
|
57 |
-
# Chat input with Enter submission
|
58 |
-
chat_input = st.text_input(
|
59 |
-
"Ask something about the corrected sentence (press Enter to send) ➡️",
|
60 |
-
key="chat_input",
|
61 |
-
value="",
|
62 |
-
on_change=lambda: submit_chat(),
|
63 |
-
)
|
64 |
-
|
65 |
-
# Function to handle chat submission with full context
|
66 |
-
def submit_chat():
|
67 |
-
chat_text = st.session_state.chat_input
|
68 |
-
if chat_text:
|
69 |
-
# Build prompt with corrected sentence and full chat history
|
70 |
-
prompt = f"The corrected sentence is: '{st.session_state.correctedSentence}'.\n"
|
71 |
-
if st.session_state.chat_history:
|
72 |
-
prompt += "Previous conversation:\n"
|
73 |
-
for speaker, message in st.session_state.chat_history:
|
74 |
-
prompt += f"{speaker}: {message}\n"
|
75 |
-
prompt += f"User asks: '{chat_text}'. Respond naturally."
|
76 |
-
|
77 |
try:
|
78 |
-
response =
|
79 |
-
|
80 |
-
|
81 |
-
max_new_tokens=150,
|
82 |
-
temperature=0.7,
|
83 |
-
)
|
84 |
-
llm_response = response.strip()
|
85 |
-
st.session_state.chat_history.append(("You", chat_text))
|
86 |
-
st.session_state.chat_history.append(("LLM", llm_response))
|
87 |
-
st.session_state.chat_input = ""
|
88 |
except Exception as e:
|
89 |
st.error(f"Error in chat: {str(e)}")
|
90 |
-
|
|
|
91 |
else:
|
92 |
-
st.write("Improve a sentence first to start chatting!")
|
93 |
-
|
94 |
-
# Optional: Clear chat button
|
95 |
-
if st.button("Clear Chat"):
|
96 |
-
st.session_state.chat_history = []
|
97 |
-
st.rerun()
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
|
|
3 |
|
4 |
+
# Cache the model to load it only once
|
5 |
+
@st.cache_resource
|
6 |
+
def load_generator():
|
7 |
+
return pipeline("text-generation", model="aubmindlab/aragpt2-base", device=-1) # device=-1 forces CPU
|
8 |
|
9 |
+
# Load the text generation pipeline
|
10 |
+
generator = load_generator()
|
|
|
|
|
|
|
11 |
|
12 |
+
# App title
|
13 |
+
st.title("Arabic Sentence Improver & Chat App")
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Sentence Correction Section
|
16 |
+
st.subheader("Improve an Arabic Sentence")
|
17 |
+
user_input = st.text_input("Enter an Arabic sentence to improve:", "أنا ذهبت الحديقة")
|
18 |
if st.button("Improve Sentence"):
|
19 |
if user_input:
|
20 |
+
# Prompt the model to correct the sentence
|
21 |
+
prompt = f"Correct this Arabic sentence: '{user_input}' to"
|
22 |
try:
|
23 |
+
response = generator(prompt, max_new_tokens=50, temperature=0.7)[0]["generated_text"]
|
24 |
+
# Extract the corrected sentence (assuming it follows "to")
|
25 |
+
corrected_sentence = response.split("to")[1].strip() if "to" in response else response
|
26 |
+
st.session_state.corrected_sentence = corrected_sentence
|
27 |
+
st.success(f"Improved Sentence: {corrected_sentence}")
|
|
|
|
|
|
|
28 |
except Exception as e:
|
29 |
st.error(f"Error: {str(e)}")
|
30 |
else:
|
31 |
st.warning("Please enter a sentence first!")
|
32 |
|
33 |
+
# Chat Section
|
34 |
st.subheader("Chat About the Corrected Sentence")
|
35 |
+
if "corrected_sentence" in st.session_state:
|
36 |
+
chat_input = st.text_input("Ask something about the corrected sentence:", key="chat_input")
|
37 |
+
if st.button("Send"):
|
38 |
+
if chat_input:
|
39 |
+
# Prompt the model with context for chatting
|
40 |
+
prompt = f"The corrected sentence is: '{st.session_state.corrected_sentence}'. User asks: '{chat_input}'"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
try:
|
42 |
+
response = generator(prompt, max_new_tokens=100, temperature=0.7)[0]["generated_text"]
|
43 |
+
st.write(f"**You:** {chat_input}")
|
44 |
+
st.write(f"**LLM:** {response}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
except Exception as e:
|
46 |
st.error(f"Error in chat: {str(e)}")
|
47 |
+
else:
|
48 |
+
st.warning("Please enter a question!")
|
49 |
else:
|
50 |
+
st.write("Improve a sentence first to start chatting!")
|
|
|
|
|
|
|
|
|
|