Spaces:
Sleeping
Sleeping
RaghadAbdulaziz
commited on
Commit
ยท
f1a747c
1
Parent(s):
ac382d7
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# โ
Load your model
|
6 |
+
model_name = "microsoft/DialoGPT-medium" # Replace with your model if different
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# โ
Keyword filter (from your notebook)
|
11 |
+
allowed_keywords = [
|
12 |
+
"ุนุฌูุฉ", "Ajwa", "ุฌุงููุณู", "Galaxy", "ู
ุฌุฏูู", "Medjool", "ู
ูููู",
|
13 |
+
"Munifi", "ุณูุฑู", "Sukkari", "ุชู
ุฑ", "ุชู
ุฑูุฉ", "ูุฎูู", "ูุฎูุฉ", "ู
ุฒุฑุนุฉ", "ุจูุญ", "ู
ุงุก", "ุฑู", "ูุฎู", "dates", "date"
|
14 |
+
]
|
15 |
+
|
16 |
+
# โ
Chat function with filtering
|
17 |
+
def chatbot_fn(message, history=[]):
|
18 |
+
# Keyword check
|
19 |
+
if not any(keyword.lower() in message.lower() for keyword in allowed_keywords):
|
20 |
+
response = "ุฃูุง ุฃุฌูุจ ููุท ุนูู ุงูุฃุณุฆูุฉ ุงูู
ุชุนููุฉ ุจุงููุฎูู ูุงูุชู
ูุฑุ ูู ูู
ููู ุชุญุฏูุฏ ุณุคุงูู ุฃูุซุฑุ"
|
21 |
+
history.append((message, response))
|
22 |
+
return history, history
|
23 |
+
|
24 |
+
# Encode and generate
|
25 |
+
inputs = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt")
|
26 |
+
outputs = model.generate(inputs, max_length=500, pad_token_id=tokenizer.eos_token_id)
|
27 |
+
response = tokenizer.decode(outputs[:, inputs.shape[-1]:][0], skip_special_tokens=True)
|
28 |
+
|
29 |
+
history.append((message, response))
|
30 |
+
return history, history
|
31 |
+
|
32 |
+
# โ
Gradio UI
|
33 |
+
demo = gr.ChatInterface(fn=chatbot_fn, title="Lina Chatbot")
|
34 |
+
|
35 |
+
demo.launch()
|