import os
import gradio as gr
import openai

# Install required libraries
os.system("pip install gradio")
os.system("pip install openai")

# Set your OpenAI API key
openai.api_key = "your_api_key_here"  # Replace with your actual API key

class ArabicAssistant:
    def generate_response(self, user_input):
        # 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 ChatGPT 3.5 model
        response = openai.Completion.create(
            model="text-davinci-003",
            prompt=user_input,
            temperature=0.7,
            max_tokens=100
        )["choices"][0]["text"].strip()

        return response

assistant = ArabicAssistant()

def chatbot_interaction(user_input):
    user_input = user_input.strip()

    # 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()