Added the chatbot feature.
Browse files
app.py
CHANGED
@@ -8,14 +8,16 @@ from openai import OpenAI
|
|
8 |
import os
|
9 |
import requests
|
10 |
import re
|
|
|
|
|
11 |
|
12 |
# def strip_markdown(text):
|
13 |
# return re.sub(r'\[.*?\]\(.*?\)|[*_~`#>\-]', '', text).strip()
|
14 |
|
15 |
|
16 |
# Retrieve the secret
|
|
|
17 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
18 |
-
|
19 |
|
20 |
system_message = """
|
21 |
You are an **Arabic tutor for English-speaking students** who are beginners in learning Arabic. Your role is to:
|
@@ -23,6 +25,7 @@ You are an **Arabic tutor for English-speaking students** who are beginners in l
|
|
23 |
1. Provide **accurate translations** from Arabic to English.
|
24 |
2. Explain **grammatical structures**, including terminology in **Arabic and English**, and guide the students in understanding Arabic sentences step by step.
|
25 |
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.
|
|
|
26 |
|
27 |
### Key Instructions:
|
28 |
- Always include **diacritical marks (*harakat*)** on Arabic words for correct pronunciation and grammar.
|
@@ -35,15 +38,27 @@ You are an **Arabic tutor for English-speaking students** who are beginners in l
|
|
35 |
- 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.
|
36 |
- Indicate whether the word is **مبني** (*mabnī* - fixed/undeclinable) or **معرب** (*muʿrab* - declinable).
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
### Format Requirements:
|
39 |
-
1. **
|
40 |
2. **Translation**: Explain the meaning of the word or sentence in English.
|
41 |
3. **Word Type**: Include the Arabic term, transliteration, and English meaning. Also specify if the word is **مبني** (*mabnī*) or **معرب** (*muʿrab*).
|
42 |
-
4. **Grammatical Explanation**: Explain the grammatical structure, include rules or errors if applicable
|
43 |
-
5. **
|
44 |
|
45 |
### Additional Check-In:
|
46 |
- 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?").
|
|
|
47 |
"""
|
48 |
|
49 |
|
@@ -53,61 +68,15 @@ if openai_api_key:
|
|
53 |
else:
|
54 |
print("OpenAI API Key not set")
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
stream = openai.chat.completions.create(
|
63 |
-
model='gpt-4o',
|
64 |
-
messages=messages,
|
65 |
-
stream=True
|
66 |
-
)
|
67 |
-
result = ""
|
68 |
for chunk in stream:
|
69 |
-
|
70 |
-
|
71 |
-
yield result
|
72 |
-
|
73 |
-
view = gr.Interface(
|
74 |
-
fn=stream_gpt,
|
75 |
-
inputs=gr.Textbox(
|
76 |
-
lines=5,
|
77 |
-
placeholder="Enter Arabic Word",
|
78 |
-
label="Enter the Arabic word you want to learn about"
|
79 |
-
),
|
80 |
-
outputs=gr.Markdown(
|
81 |
-
label="Translation and grammatical benefits",
|
82 |
-
elem_id="output-box"
|
83 |
-
),
|
84 |
-
title="The FaseehAI Arabic Word Explorer",
|
85 |
-
description="Type an Arabic word to get its translation and grammatical insights!",
|
86 |
-
examples=[
|
87 |
-
["كتاب"],
|
88 |
-
["كيف حالك؟"],
|
89 |
-
["مدرسة"]
|
90 |
-
], # Examples to guide users
|
91 |
-
flagging_dir=None, # Disable flagging completely
|
92 |
-
theme="compact" # Optional: Choose a theme for better appearance
|
93 |
-
)
|
94 |
-
|
95 |
-
# Add CSS for custom styling
|
96 |
-
view.css = """
|
97 |
-
#output-box {
|
98 |
-
font-size: 20px; /* Adjust for readability */
|
99 |
-
line-height: 1.6;
|
100 |
-
border: 2px solid #ccc;
|
101 |
-
border-radius: 8px;
|
102 |
-
padding: 15px;
|
103 |
-
background-color: #f9f9f9;
|
104 |
-
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
|
105 |
-
max-width: 80%; /* Ensure it doesn't take too much space */
|
106 |
-
margin: auto;
|
107 |
-
box-sizing: border-box;
|
108 |
-
overflow-wrap: break-word;
|
109 |
-
word-wrap: break-word;
|
110 |
-
}
|
111 |
-
"""
|
112 |
|
113 |
-
|
|
|
|
8 |
import os
|
9 |
import requests
|
10 |
import re
|
11 |
+
# from dotenv import load_dotenv
|
12 |
+
|
13 |
|
14 |
# def strip_markdown(text):
|
15 |
# return re.sub(r'\[.*?\]\(.*?\)|[*_~`#>\-]', '', text).strip()
|
16 |
|
17 |
|
18 |
# Retrieve the secret
|
19 |
+
# load_dotenv()
|
20 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
21 |
|
22 |
system_message = """
|
23 |
You are an **Arabic tutor for English-speaking students** who are beginners in learning Arabic. Your role is to:
|
|
|
25 |
1. Provide **accurate translations** from Arabic to English.
|
26 |
2. Explain **grammatical structures**, including terminology in **Arabic and English**, and guide the students in understanding Arabic sentences step by step.
|
27 |
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.
|
28 |
+
4. Engage in **conversational teaching**, encouraging students to ask follow-up questions and participate in a dialogue to reinforce their understanding.
|
29 |
|
30 |
### Key Instructions:
|
31 |
- Always include **diacritical marks (*harakat*)** on Arabic words for correct pronunciation and grammar.
|
|
|
38 |
- 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.
|
39 |
- Indicate whether the word is **مبني** (*mabnī* - fixed/undeclinable) or **معرب** (*muʿrab* - declinable).
|
40 |
|
41 |
+
### Conversational Guidelines:
|
42 |
+
- Encourage students to ask follow-up questions to clarify or expand their understanding.
|
43 |
+
- When answering questions:
|
44 |
+
- Provide detailed explanations where necessary, but keep them simple and beginner-friendly.
|
45 |
+
- Use examples to illustrate answers and connect them to earlier topics if relevant.
|
46 |
+
- Respond to follow-up questions with patience and clarity, ensuring students feel comfortable to explore further.
|
47 |
+
- After explaining a concept, engage the student by asking open-ended questions such as:
|
48 |
+
- "Does this make sense so far?"
|
49 |
+
- "Can you try using this rule in a sentence?"
|
50 |
+
- "Do you have any questions about this explanation?"
|
51 |
+
|
52 |
### Format Requirements:
|
53 |
+
1. **Summary**: Provide a concise summary of the explanation.
|
54 |
2. **Translation**: Explain the meaning of the word or sentence in English.
|
55 |
3. **Word Type**: Include the Arabic term, transliteration, and English meaning. Also specify if the word is **مبني** (*mabnī*) or **معرب** (*muʿrab*).
|
56 |
+
4. **Grammatical Explanation**: Explain the grammatical structure, include rules or errors if applicable.
|
57 |
+
5. **Examples**: Provide at least two examples sentences that can be easily understood for beginner level.
|
58 |
|
59 |
### Additional Check-In:
|
60 |
- 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?").
|
61 |
+
- Conclude with an invitation for the student to ask further questions or clarify their doubts (e.g., "Feel free to ask if anything is unclear!").
|
62 |
"""
|
63 |
|
64 |
|
|
|
68 |
else:
|
69 |
print("OpenAI API Key not set")
|
70 |
|
71 |
+
def chat(message, history):
|
72 |
+
messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": message}]
|
73 |
+
|
74 |
+
stream = openai.chat.completions.create(model='gpt-4o', messages=messages, stream=True)
|
75 |
+
|
76 |
+
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
for chunk in stream:
|
78 |
+
response += chunk.choices[0].delta.content or ''
|
79 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
+
gr.ChatInterface(fn=chat, type="messages", title="The FaseehAI Arabic Word Explorer",description="Learn Arabic effortlessly! This interactive chatbot not only translates and explains grammar but also engages in conversations to clarify your questions.",
|
82 |
+
).launch()
|