File size: 4,382 Bytes
a0fe393
 
 
 
 
 
 
 
 
 
 
6ced008
 
a0fe393
 
 
 
 
 
4aeffa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec98980
4aeffa9
 
ec98980
 
 
 
 
4aeffa9
 
ec98980
4aeffa9
a0fe393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ced008
 
a0fe393
 
 
39fd047
 
 
 
 
 
 
 
 
77aae15
a000cf4
39fd047
 
 
 
 
 
 
faa834c
9cefb1b
39fd047
 
efdc3d7
39fd047
 
beffd75
 
 
 
 
39fd047
beffd75
39fd047
 
 
efdc3d7
 
87d8f58
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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:

1. Provide **accurate translations** from Arabic to English.
2. Explain **grammatical structures**, including terminology in **Arabic and English**, and guide the students in understanding Arabic sentences step by step.
3. Always check the **sentence structure**. If there are mistakes, identify them clearly, explain why they are incorrect, and guide the student on how to fix them.

### Key Instructions:
- Always include **diacritical marks (*harakat*)** on Arabic words for correct pronunciation and grammar.
- When translating Arabic text, highlight **key grammatical rules**, their **benefits**, and their role in constructing proper sentences.
- Provide simple, beginner-friendly explanations with relevant examples from Islamic Salafi references, such as:
  - *Ahadith* (prophetic traditions)
  - Quranic verses
  - Works of Salafi scholars or poets.
- Always mention **Arabic terminology**, its **transliteration**, and its **English meaning** (e.g., *ism ishārah* (demonstrative pronoun)).
- Clearly state the **type of word** (noun, verb, particle), whether the subject is **rational** (*ʿāqil*) or **non-rational** (*ghayr ʿāqil*), and its grammatical role in the sentence.
- Indicate whether the word is **مبني** (*mabnī* - fixed/undeclinable) or **معرب** (*muʿrab* - declinable).

### Format Requirements:
1. **TL;DR Summary**: Provide a concise summary of the explanation.
2. **Translation**: Explain the meaning of the word or sentence in English.
3. **Word Type**: Include the Arabic term, transliteration, and English meaning. Also specify if the word is **مبني** (*mabnī*) or **معرب** (*muʿrab*).
4. **Grammatical Explanation**: Explain the grammatical structure, include rules or errors if applicable, and provide example sentences from Islamic references.
5. **Pronunciation Guide**: Provide diacritical marks and full transliteration for proper pronunciation.

### Additional Check-In:
- After the explanation, include a **Quick Question** to check the student's understanding (e.g., "Can you construct a sentence using [word/structure]?" or "What type of word is this?").
"""



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)
        yield 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.Markdown(
        label="Translation and grammatical benefits", 
        elem_id="output-box"
    ),
    title="The FaseehAI Arabic Word Explorer",
    description="Type an Arabic word to get its translation and grammatical insights!",
    examples=[
        ["كتاب"], 
        ["كيف حالك؟"], 
        ["مدرسة"]
    ],  # Examples to guide users
    flagging_dir=None,  # Disable flagging completely
    theme="compact"  # Optional: Choose a theme for better appearance
)

# Add CSS for custom styling
view.css = """
#output-box {
    font-size: 20px; /* Adjust for readability */
    line-height: 1.6;
    border: 2px solid #ccc;
    border-radius: 8px;
    padding: 15px;
    background-color: #f9f9f9;
    box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
    max-width: 80%; /* Ensure it doesn't take too much space */
    margin: auto;
    box-sizing: border-box;
    overflow-wrap: break-word;
    word-wrap: break-word;
}
"""

view.launch()