Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,61 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
-
# Set
|
| 5 |
-
st.
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
}
|
| 20 |
-
|
| 21 |
-
""", unsafe_allow_html=True)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
if "
|
| 28 |
-
st.session_state.
|
| 29 |
-
|
| 30 |
-
# Display chat history
|
| 31 |
-
for message in st.session_state.messages:
|
| 32 |
-
with st.chat_message(message["role"]):
|
| 33 |
-
st.markdown(message["content"], unsafe_allow_html=True)
|
| 34 |
|
| 35 |
# User input
|
| 36 |
-
user_input = st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
st.
|
| 42 |
-
|
| 43 |
-
# Tokenize input and generate response
|
| 44 |
-
inputs = tokenizer.encode(user_input, return_tensors="pt")
|
| 45 |
-
outputs = model.generate(inputs, max_length=100)
|
| 46 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 47 |
-
|
| 48 |
-
# Add AI response to history
|
| 49 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 50 |
-
|
| 51 |
-
# Rerun to update the UI
|
| 52 |
-
st.rerun()
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
| 3 |
+
# Set the title of the app
|
| 4 |
+
st.title("Urdu AI Chatbot 🤖")
|
| 5 |
+
|
| 6 |
+
# Define intents and responses (keyword-based)
|
| 7 |
+
intents = {
|
| 8 |
+
"greeting": {
|
| 9 |
+
"keywords": ["ہیلو", "سلام", "آداب", "حال", "کیسے ہو", "کیسے ہیں"],
|
| 10 |
+
"response": "السلام علیکم! میں ٹھیک ہوں، شکریہ۔ آپ کیسے ہیں؟"
|
| 11 |
+
},
|
| 12 |
+
"farewell": {
|
| 13 |
+
"keywords": ["الوداع", "خدا حافظ", "بعد میں ملتے ہیں"],
|
| 14 |
+
"response": "خدا حافظ! آپ کا دن اچھا گزرے۔"
|
| 15 |
+
},
|
| 16 |
+
"thanks": {
|
| 17 |
+
"keywords": ["شکریہ", "بہت شکریہ", "مہربانی"],
|
| 18 |
+
"response": "خوش آمدید! کیا میں آپ کی مزید مدد کر سکتا ہوں؟"
|
| 19 |
}
|
| 20 |
+
}
|
|
|
|
| 21 |
|
| 22 |
+
# Function to detect intent using keywords
|
| 23 |
+
def detect_intent(user_input):
|
| 24 |
+
user_input = user_input.lower()
|
| 25 |
+
for intent, data in intents.items():
|
| 26 |
+
for keyword in data["keywords"]:
|
| 27 |
+
if keyword in user_input:
|
| 28 |
+
return intent
|
| 29 |
+
return "unknown"
|
| 30 |
|
| 31 |
+
# Initialize chat history
|
| 32 |
+
if "chat_history" not in st.session_state:
|
| 33 |
+
st.session_state.chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# User input
|
| 36 |
+
user_input = st.text_input("آپ کا پیغام یہاں لکھیں:", "ہیلو، آپ کیسے ہیں؟")
|
| 37 |
+
|
| 38 |
+
# Send button
|
| 39 |
+
if st.button("بھیجیں"):
|
| 40 |
+
if user_input:
|
| 41 |
+
# Add user message to chat history
|
| 42 |
+
st.session_state.chat_history.append(f"آپ: {user_input}")
|
| 43 |
+
|
| 44 |
+
# Detect intent using keywords (no model needed)
|
| 45 |
+
intent = detect_intent(user_input)
|
| 46 |
+
|
| 47 |
+
# Get response based on intent
|
| 48 |
+
if intent != "unknown":
|
| 49 |
+
response = intents[intent]["response"]
|
| 50 |
+
else:
|
| 51 |
+
response = "معذرت، میں اس سوال کا جواب نہیں دے سکتا۔ براہ کرم ایک آسان سوال پوچھیں۔"
|
| 52 |
+
|
| 53 |
+
# Add AI response to chat history
|
| 54 |
+
st.session_state.chat_history.append(f"AI: {response}")
|
| 55 |
+
else:
|
| 56 |
+
st.warning("براہ کرم ایک پیغام لکھیں۔")
|
| 57 |
|
| 58 |
+
# Display chat history
|
| 59 |
+
st.write("چیٹ کی تاریخ:")
|
| 60 |
+
for message in st.session_state.chat_history:
|
| 61 |
+
st.write(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|