MonsterBoyTabs commited on
Commit
eb084ba
·
verified ·
1 Parent(s): a6efc63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -44
app.py CHANGED
@@ -1,52 +1,61 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
- # Set page config for Urdu (right-to-left support)
5
- st.set_page_config(page_title="اردو AI", layout="centered")
6
-
7
- # Load the Urdu model and tokenizer
8
- model_name = "m3hrdadfi/mt5-small-finetuned-urdu-sentiment"
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
11
-
12
- # Custom CSS for Urdu font and RTL support
13
- st.markdown("""
14
- <style>
15
- body {
16
- font-family: "Noto Sans Urdu", sans-serif;
17
- direction: rtl;
18
- text-align: right;
 
19
  }
20
- </style>
21
- """, unsafe_allow_html=True)
22
 
23
- # Title
24
- st.title("🕌 اردو AI مددگار")
 
 
 
 
 
 
25
 
26
- # Chat history
27
- if "messages" not in st.session_state:
28
- st.session_state.messages = []
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.chat_input("اپنا پیغام یہاں لکھیں...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- # Generate response
39
- if user_input:
40
- # Add user message to history
41
- st.session_state.messages.append({"role": "user", "content": user_input})
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)