Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,40 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import streamlit.components.v1 as components
|
| 3 |
-
from transformers import
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def main():
|
| 7 |
-
# تكوين الصفحة
|
| 8 |
st.set_page_config(
|
| 9 |
page_title="سبيدي",
|
| 10 |
page_icon="💬",
|
|
@@ -21,60 +51,41 @@ def main():
|
|
| 21 |
"""
|
| 22 |
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
# قراءة ملف HTML
|
| 25 |
def read_html():
|
| 26 |
with open('index.html', 'r', encoding='utf-8') as file:
|
| 27 |
return file.read()
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
def
|
| 37 |
-
|
| 38 |
-
response =
|
| 39 |
-
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
result = components.html(
|
| 47 |
-
read_html() + """
|
| 48 |
-
<script>
|
| 49 |
-
// تعديل دالة sendMessage لتتواصل مع Python
|
| 50 |
-
async function sendMessage() {
|
| 51 |
-
var input = document.getElementById('message-input');
|
| 52 |
-
var message = input.value.trim();
|
| 53 |
-
if (message !== '') {
|
| 54 |
-
appendMessage('user-message', message);
|
| 55 |
-
input.value = '';
|
| 56 |
-
|
| 57 |
-
// إرسال الرسالة إلى Python
|
| 58 |
-
const response = await fetch('/_stcore/message', {
|
| 59 |
-
method: 'POST',
|
| 60 |
-
headers: {'Content-Type': 'application/json'},
|
| 61 |
-
body: JSON.stringify({message: message})
|
| 62 |
-
});
|
| 63 |
-
|
| 64 |
-
const data = await response.json();
|
| 65 |
-
appendMessage('bot-message', data.response);
|
| 66 |
-
}
|
| 67 |
-
}
|
| 68 |
-
</script>
|
| 69 |
-
""",
|
| 70 |
-
height=800
|
| 71 |
)
|
| 72 |
-
|
| 73 |
-
# معالجة الرسائل المستلمة من JavaScript
|
| 74 |
-
if st.session_state.messages:
|
| 75 |
-
last_message = st.session_state.messages[-1]
|
| 76 |
-
response = process_message(last_message)
|
| 77 |
-
st.session_state.messages.append(response)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import streamlit.components.v1 as components
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
import json
|
| 6 |
+
from streamlit.runtime.scriptrunner import add_script_run_ctx
|
| 7 |
+
import threading
|
| 8 |
+
|
| 9 |
+
# تكوين النموذج والتوكنايزر
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained("amd/AMD-OLMo-1B")
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
"amd/AMD-OLMo-1B",
|
| 15 |
+
torch_dtype=torch.float16,
|
| 16 |
+
device_map="auto"
|
| 17 |
+
)
|
| 18 |
+
return model, tokenizer
|
| 19 |
+
|
| 20 |
+
def generate_response(prompt, model, tokenizer):
|
| 21 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model.generate(
|
| 25 |
+
**inputs,
|
| 26 |
+
max_length=200,
|
| 27 |
+
num_return_sequences=1,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
top_p=0.9,
|
| 30 |
+
repetition_penalty=1.2,
|
| 31 |
+
pad_token_id=tokenizer.eos_token_id
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
+
return response.replace(prompt, "").strip()
|
| 36 |
|
| 37 |
def main():
|
|
|
|
| 38 |
st.set_page_config(
|
| 39 |
page_title="سبيدي",
|
| 40 |
page_icon="💬",
|
|
|
|
| 51 |
"""
|
| 52 |
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
| 53 |
|
| 54 |
+
# تحميل النموذج والتوكنايزر
|
| 55 |
+
model, tokenizer = load_model()
|
| 56 |
+
|
| 57 |
# قراءة ملف HTML
|
| 58 |
def read_html():
|
| 59 |
with open('index.html', 'r', encoding='utf-8') as file:
|
| 60 |
return file.read()
|
| 61 |
|
| 62 |
+
# معالجة الرسائل الواردة من JavaScript
|
| 63 |
+
def handle_message(message_data):
|
| 64 |
+
try:
|
| 65 |
+
data = json.loads(message_data)
|
| 66 |
+
user_message = data.get('message', '')
|
| 67 |
+
|
| 68 |
+
if user_message:
|
| 69 |
+
response = generate_response(user_message, model, tokenizer)
|
| 70 |
+
return {"response": response}
|
| 71 |
+
|
| 72 |
+
return {"response": "عذراً، لم أفهم رسالتك"}
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return {"response": f"عذراً، حدث خطأ: {str(e)}"}
|
| 76 |
|
| 77 |
+
# تكوين معالج الرسائل
|
| 78 |
+
def message_handler(message_data):
|
| 79 |
+
ctx = add_script_run_ctx()
|
| 80 |
+
response = handle_message(message_data)
|
| 81 |
+
ctx.enqueue(json.dumps(response))
|
| 82 |
|
| 83 |
+
# عرض الواجهة
|
| 84 |
+
components.html(
|
| 85 |
+
read_html(),
|
| 86 |
+
height=800,
|
| 87 |
+
on_message=message_handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
main()
|