mosama commited on
Commit
5c3b5c3
Β·
verified Β·
1 Parent(s): 4bd8aab

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import torch
4
+
5
+ @st.cache_resource(show_spinner="Loading Model & Tokenizer")
6
+ def load_model():
7
+ # This is cached and will not run again and again.
8
+ from transformers import AutoTokenizer, AutoModelForCausalLM
9
+ import torch
10
+
11
+ base_model = AutoModelForCausalLM.from_pretrained(
12
+ "mosama/Qwen2.5-0.5B-Pretrained-ar-end-urd-500", device_map="cpu", torch_dtype=torch.float16)
13
+
14
+ tokenizer = AutoTokenizer.from_pretrained("mosama/Qwen2.5-0.5B-Pretrained-ar-end-urd-500")
15
+ st.success('Model & Tokenizer Loaded Successfully!', icon="βœ…")
16
+ return base_model, tokenizer
17
+
18
+ st.title("Qwen2.5-0.5B Arabic, English & Urdu Continuous Pretrained")
19
+
20
+ model, tokenizer = load_model()
21
+
22
+ # Initialize chat history
23
+ if "messages" not in st.session_state:
24
+ st.session_state.messages = []
25
+
26
+ for message in st.session_state.messages:
27
+ with st.chat_message(message["role"]):
28
+ st.markdown(message["content"])
29
+
30
+ if not st.session_state.messages:
31
+ with st.chat_message("assistant", avatar="assistant"):
32
+ st.write("Hello πŸ‘‹ I am an AI bot powered by Qwen 2.5 0.5B model.")
33
+ st.session_state.messages.append({"role": "assistant", "content": "Hello πŸ‘‹ I am an AI bot powered by Qwen 2.5 0.5B model."})
34
+
35
+ if prompt := st.chat_input("Say Something"):
36
+ # Display user message in chat message container
37
+ with st.chat_message("user"):
38
+ st.markdown(prompt)
39
+ # Add user message to chat history
40
+ st.session_state.messages.append({"role": "user", "content": prompt})
41
+
42
+ if prompt:
43
+ with st.spinner(text="Generating response..."):
44
+ model_inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
45
+ print(model_inputs)
46
+ generated_ids = model.generate(
47
+ **model_inputs,
48
+ max_new_tokens=50,
49
+ repetition_penalty=1.2,
50
+ temperature=0.5,
51
+ do_sample=True,
52
+ top_p=0.9,
53
+ top_k=20
54
+ )
55
+ print("Generated Response!")
56
+ response = tokenizer.decode(generated_ids, skip_special_tokens=True)[0]
57
+
58
+ # Display assistant response in chat message container
59
+ with st.chat_message("assistant"):
60
+ st.markdown(response)
61
+ # Add assistant response to chat history
62
+ st.session_state.messages.append({"role": "assistant", "content": response})