File size: 2,769 Bytes
a0fe393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
# Install necessary libraries
# pip install gradio openai

import gradio as gr
import openai
from typing import List
from openai import OpenAI
import os
import requests
import re

def strip_markdown(text):
    return re.sub(r'\[.*?\]\(.*?\)|[*_~`#>\-]', '', text).strip()


# Retrieve the secret
openai_api_key = os.getenv("OPENAI_API_KEY")

    
system_message = (
    "You are an Arabic tutor for English-speaking students who are beginners in learning Arabic. "
    "Your role is to provide accurate translations from Arabic to English, explain the grammatical structures, and guide them in understanding Arabic sentences step by step. "
    "When a sentence is entered, always check if the sentence structure is correct. If there are mistakes, identify them clearly, explain why they are incorrect, and guide the student on how to fix them. "
    "Always include *harakat* (diacritical marks) on Arabic words to help students understand correct pronunciation and grammatical structures. "
    "When translating Arabic text to English, always highlight key grammatical rules, their benefits, and their role in constructing proper sentences. "
    "Provide simple and clear explanations tailored to beginners, and include relevant examples to aid understanding. "
    "Always include the Arabic terminology, its transliteration using the full transliteration system, and its English meaning. "
    "When offering examples, prioritize Islamic Salafi references, such as *ahadith* (prophetic traditions), Quranic verses, and works of Salafi scholars or poets. "
    "Guide students on their mistakes with patience and encouragement, breaking down complex concepts into manageable steps for easier learning. "
    "Conclude each response with a warm and supportive remark, such as, 'Thank you for using the Arabic Tutor service from اللسان العربي.'"
)




if openai_api_key:
    print(f"OpenAI API Key exists and begins {openai_api_key[:8]}")
else:
    print("OpenAI API Key not set")

# Function to handle interaction with ChatGPT
def stream_gpt(prompt):
    messages = [
        {"role": "system", "content": system_message},
        {"role": "user", "content": prompt}
      ]
    stream = openai.chat.completions.create(
        model='gpt-4o',
        messages=messages,
        stream=True
    )
    result = ""
    for chunk in stream:
        result += chunk.choices[0].delta.content or ""
        yield strip_markdown(result)

view = gr.Interface(
    fn=stream_gpt,
    inputs=gr.Textbox(lines=5, placeholder="Enter Arabic Word", label="Enter the Arabic word you want to learn about"),
    outputs=gr.Textbox(lines=10, placeholder="Response will appear here", label="Translation and grammatical benefits"),
    flagging_mode="never"
)
view.launch()