File size: 2,191 Bytes
bcff14c
 
0bfb1ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcff14c
0bfb1ee
 
d6fb17d
0bfb1ee
 
 
 
 
32bd47a
0bfb1ee
 
 
 
 
 
 
 
 
 
 
 
 
bcff14c
eb084ba
0bfb1ee
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st

# Set the title of the app
st.title("Urdu AI Chatbot 🤖")

# Define intents and responses (keyword-based)
intents = {
    "greeting": {
        "keywords": ["ہیلو", "سلام", "آداب", "حال", "کیسے ہو", "کیسے ہیں"],
        "response": "السلام علیکم! میں ٹھیک ہوں، شکریہ۔ آپ کیسے ہیں؟"
    },
    "farewell": {
        "keywords": ["الوداع", "خدا حافظ", "بعد میں ملتے ہیں"],
        "response": "خدا حافظ! آپ کا دن اچھا گزرے۔"
    },
    "thanks": {
        "keywords": ["شکریہ", "بہت شکریہ", "مہربانی"],
        "response": "خوش آمدید! کیا میں آپ کی مزید مدد کر سکتا ہوں؟"
    }
}

# Function to detect intent using keywords
def detect_intent(user_input):
    user_input = user_input.lower()
    for intent, data in intents.items():
        for keyword in data["keywords"]:
            if keyword in user_input:
                return intent
    return "unknown"

# Initialize chat history
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []

# User input
user_input = st.text_input("آپ کا پیغام یہاں لکھیں:", "ہیلو، آپ کیسے ہیں؟")

# Send button
if st.button("بھیجیں"):
    if user_input:
        # Add user message to chat history
        st.session_state.chat_history.append(f"آپ: {user_input}")
        
        # Detect intent using keywords (no model needed)
        intent = detect_intent(user_input)
        
        # Get response based on intent
        if intent != "unknown":
            response = intents[intent]["response"]
        else:
            response = "معذرت، میں اس سوال کا جواب نہیں دے سکتا۔ براہ کرم ایک آسان سوال پوچھیں۔"
        
        # Add AI response to chat history
        st.session_state.chat_history.append(f"AI: {response}")
    else:
        st.warning("براہ کرم ایک پیغام لکھیں۔")

# Display chat history
st.write("چیٹ کی تاریخ:")
for message in st.session_state.chat_history:
    st.write(message)