mmshaban commited on
Commit
a0fe393
·
verified ·
1 Parent(s): 292de75

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install necessary libraries
2
+ # pip install gradio openai
3
+
4
+ import gradio as gr
5
+ import openai
6
+ from typing import List
7
+ 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. "
22
+ "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. "
23
+ "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. "
24
+ "Always include *harakat* (diacritical marks) on Arabic words to help students understand correct pronunciation and grammatical structures. "
25
+ "When translating Arabic text to English, always highlight key grammatical rules, their benefits, and their role in constructing proper sentences. "
26
+ "Provide simple and clear explanations tailored to beginners, and include relevant examples to aid understanding. "
27
+ "Always include the Arabic terminology, its transliteration using the full transliteration system, and its English meaning. "
28
+ "When offering examples, prioritize Islamic Salafi references, such as *ahadith* (prophetic traditions), Quranic verses, and works of Salafi scholars or poets. "
29
+ "Guide students on their mistakes with patience and encouragement, breaking down complex concepts into manageable steps for easier learning. "
30
+ "Conclude each response with a warm and supportive remark, such as, 'Thank you for using the Arabic Tutor service from اللسان العربي.'"
31
+ )
32
+
33
+
34
+
35
+
36
+ if openai_api_key:
37
+ print(f"OpenAI API Key exists and begins {openai_api_key[:8]}")
38
+ else:
39
+ print("OpenAI API Key not set")
40
+
41
+ # Function to handle interaction with ChatGPT
42
+ def stream_gpt(prompt):
43
+ messages = [
44
+ {"role": "system", "content": system_message},
45
+ {"role": "user", "content": prompt}
46
+ ]
47
+ stream = openai.chat.completions.create(
48
+ model='gpt-4o',
49
+ messages=messages,
50
+ stream=True
51
+ )
52
+ result = ""
53
+ for chunk in stream:
54
+ result += chunk.choices[0].delta.content or ""
55
+ yield strip_markdown(result)
56
+
57
+ view = gr.Interface(
58
+ fn=stream_gpt,
59
+ inputs=gr.Textbox(lines=5, placeholder="Enter Arabic Word", label="Enter the Arabic word you want to learn about"),
60
+ outputs=gr.Textbox(lines=10, placeholder="Response will appear here", label="Translation and grammatical benefits"),
61
+ flagging_mode="never"
62
+ )
63
+ view.launch()