import os import gradio as gr from transformers import GPT2LMHeadModel, GPT2Tokenizer import torch # Clone the Gradio repository os.system("git clone https://github.com/gradio-app/gradio.git") # Install Gradio os.system("cd gradio && pip install .") # Install transformers os.system("pip install transformers") # Install PyTorch os.system("pip install torch==1.10.0+cpu torchvision==0.11.1+cpu torchaudio==0.10.0+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html") class ArabicAssistant: def __init__(self): self.model_name = "EleutherAI/gpt-neo-2.7B" self.tokenizer = GPT2Tokenizer.from_pretrained(self.model_name) self.model = GPT2LMHeadModel.from_pretrained(self.model_name) def generate_response(self, input_text): input_ids = self.tokenizer.encode(input_text, return_tensors="pt") output = self.model.generate(input_ids, max_length=100, num_beams=5, no_repeat_ngram_size=2, top_k=50, top_p=0.95, temperature=0.7) response = self.tokenizer.decode(output[0], skip_special_tokens=True) return response assistant = ArabicAssistant() def chatbot_interaction(user_input): user_input = user_input.strip() # Check if the user input is in Arabic-Egyptian dialect if not any(char in "اأإآبتثجحخدذرزسشصضطظعغفقكلمنهوي" for char in user_input): return "عذرًا، أنا أتحدث فقط باللهجة المصرية. يرجى استخدام اللهجة المصرية في التفاعل معي." # Generate response using the chatbot model response = assistant.generate_response(user_input) # Add an open-ended question to encourage further conversation response += " أهلاً! كيف يمكنني مساعدتك اليوم؟" return response iface = gr.Interface(fn=chatbot_interaction, inputs=gr.Textbox(), outputs=gr.Textbox(), live=True, theme="huggingface", layout="vertical", title="Arabic Assistant Chatbot", description="Chat with the Arabic Assistant in the Egyptian dialect.") iface.launch()