Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,59 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
# Set
|
| 4 |
-
st.
|
| 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 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Display chat history
|
| 59 |
-
st.
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import urduhack
|
| 4 |
|
| 5 |
+
# Set page configuration
|
| 6 |
+
st.set_page_config(page_title="Urdu AI Chatbot", page_icon="🤖")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Title and description
|
| 9 |
+
st.title("اردو AI چیٹ بوٹ")
|
| 10 |
+
st.write("یہ ایک سادہ چیٹ بوٹ ہے جو اردو زبان میں جواب دیتا ہے۔ اپنا سوال درج کریں اور جواب حاصل کریں!")
|
| 11 |
+
|
| 12 |
+
# Load the Urdu model from Hugging Face
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_model():
|
| 15 |
+
# Using urduhack/roberta-urdu-small for fill-mask task as an example
|
| 16 |
+
model = pipeline("fill-mask", model="urduhack/roberta-urdu-small", tokenizer="urduhack/roberta-urdu-small")
|
| 17 |
+
return model
|
| 18 |
+
|
| 19 |
+
model = load_model()
|
| 20 |
+
|
| 21 |
+
# Function to process input and generate response
|
| 22 |
+
def generate_response(user_input):
|
| 23 |
+
try:
|
| 24 |
+
# Replace [MASK] in the input for the fill-mask task
|
| 25 |
+
if "[MASK]" not in user_input:
|
| 26 |
+
user_input = user_input + " [MASK]"
|
| 27 |
+
result = model(user_input)
|
| 28 |
+
# Return the top prediction
|
| 29 |
+
return result[0]["sequence"]
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"معذرت، کچھ غلطی ہوئی: {str(e)}"
|
| 32 |
+
|
| 33 |
+
# Chat interface
|
| 34 |
+
if "messages" not in st.session_state:
|
| 35 |
+
st.session_state.messages = [{"role": "assistant", "content": "سلام! میں آپ کی مدد کیسے کر سکتا ہوں؟"}]
|
| 36 |
|
| 37 |
# Display chat history
|
| 38 |
+
for message in st.session_state.messages:
|
| 39 |
+
with st.chat_message(message["role"]):
|
| 40 |
+
st.write(message["content"])
|
| 41 |
+
|
| 42 |
+
# User input
|
| 43 |
+
if prompt := st.chat_input("اپنا سوال یہاں لکھیں..."):
|
| 44 |
+
# Add user message to chat history
|
| 45 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 46 |
+
with st.chat_message("user"):
|
| 47 |
+
st.write(prompt)
|
| 48 |
+
|
| 49 |
+
# Generate and display assistant response
|
| 50 |
+
with st.chat_message("assistant"):
|
| 51 |
+
with st.spinner("غور کر رہا ہوں..."):
|
| 52 |
+
response = generate_response(prompt)
|
| 53 |
+
st.write(response)
|
| 54 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 55 |
+
|
| 56 |
+
# Sidebar with additional info
|
| 57 |
+
st.sidebar.title("معلومات")
|
| 58 |
+
st.sidebar.write("یہ چیٹ بوٹ Hugging Face کے `urduhack/roberta-urdu-small` ماڈل کا استعمال کرتا ہے۔")
|
| 59 |
+
st.sidebar.write("مزید ماڈلز کے لیے [Hugging Face](https://huggingface.co/models) دیکھیں۔")
|