Bofandra commited on
Commit
e39741f
Β·
verified Β·
1 Parent(s): a3d1c69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -24
app.py CHANGED
@@ -3,25 +3,28 @@ import gradio as gr
3
  import json
4
  from huggingface_hub import InferenceClient
5
 
6
- # Load environment token
7
  HF_TOKEN = os.getenv("HF_API_TOKEN")
 
 
8
  client = InferenceClient(
9
  model="deepseek-ai/DeepSeek-V3",
10
  token=HF_TOKEN
11
  )
12
 
13
- # Load word and language data
14
  with open("top_500_quran_lemmas_fixed.json", encoding="utf-8") as f:
15
  word_list = json.load(f)
16
 
17
  with open("language_list.json", encoding="utf-8") as f:
18
  language_list = json.load(f)
19
 
 
20
  word_options = [f"{word['text']} ({word['english']})" for word in word_list]
21
  language_options = [f"{lang['name']} ({lang['code']})" for lang in language_list]
22
 
23
 
24
- def create_messages(word_entry, language_code):
25
  return [
26
  {
27
  "role": "system",
@@ -30,13 +33,13 @@ def create_messages(word_entry, language_code):
30
  {
31
  "role": "user",
32
  "content": f"""
33
- Explain the Quranic word "{word_entry['text']}" (which means "{word_entry['english']}") in {language_code}.
34
 
35
  Please include:
36
- 1. Translation in {language_code}
37
  2. Root word and derivatives
38
  3. Occurrences in the Qur'an (Surah & Verse)
39
- 4. Explanation of each occurrence using easy language
40
  """,
41
  },
42
  ]
@@ -44,36 +47,44 @@ Please include:
44
 
45
  def process(word_label, lang_label):
46
  selected_word = next((w for w in word_list if w['text'] in word_label), None)
47
- language_code = lang_label.split("(")[-1].strip(")")
48
 
49
  if not selected_word:
50
- return "❌ Word not found."
 
51
 
52
- messages = create_messages(selected_word, language_code)
53
 
54
  try:
55
- response = client.chat.completions.create(
56
  messages=messages,
57
- temperature=0.7,
58
- top_p=0.9,
59
- max_tokens=1024,
60
- stream=False
61
  )
62
- return response.choices[0].message.content
 
 
 
 
 
63
 
64
  except Exception as e:
65
- return f"❌ Error: {e}"
66
 
67
 
68
- # Gradio UI
69
  with gr.Blocks() as demo:
70
- gr.Markdown("## Quran Word Info (Powered by DeepSeek-V3)")
 
71
  with gr.Row():
72
- word_input = gr.Dropdown(choices=word_options, label="Select Word")
73
- lang_input = gr.Dropdown(choices=language_options, label="Select Language")
74
- run_btn = gr.Button("Get Info")
75
- output = gr.Textbox(lines=20, label="Response")
 
76
 
77
- run_btn.click(fn=process, inputs=[word_input, lang_input], outputs=output)
78
 
79
- demo.launch()
 
3
  import json
4
  from huggingface_hub import InferenceClient
5
 
6
+ # Load your Hugging Face token from environment variable
7
  HF_TOKEN = os.getenv("HF_API_TOKEN")
8
+
9
+ # Initialize the inference client
10
  client = InferenceClient(
11
  model="deepseek-ai/DeepSeek-V3",
12
  token=HF_TOKEN
13
  )
14
 
15
+ # Load Quran words and language list
16
  with open("top_500_quran_lemmas_fixed.json", encoding="utf-8") as f:
17
  word_list = json.load(f)
18
 
19
  with open("language_list.json", encoding="utf-8") as f:
20
  language_list = json.load(f)
21
 
22
+ # Prepare dropdown options
23
  word_options = [f"{word['text']} ({word['english']})" for word in word_list]
24
  language_options = [f"{lang['name']} ({lang['code']})" for lang in language_list]
25
 
26
 
27
+ def create_messages(word_entry, language_name):
28
  return [
29
  {
30
  "role": "system",
 
33
  {
34
  "role": "user",
35
  "content": f"""
36
+ Explain the Quranic word "{word_entry['text']}" (which means "{word_entry['english']}") in {language_name}.
37
 
38
  Please include:
39
+ 1. Translation in {language_name}
40
  2. Root word and derivatives
41
  3. Occurrences in the Qur'an (Surah & Verse)
42
+ 4. Explanation of each occurrence using easy-to-understand {language_name}
43
  """,
44
  },
45
  ]
 
47
 
48
  def process(word_label, lang_label):
49
  selected_word = next((w for w in word_list if w['text'] in word_label), None)
50
+ language_name = lang_label.split("(")[1].strip()
51
 
52
  if not selected_word:
53
+ yield "❌ Word not found."
54
+ return
55
 
56
+ messages = create_messages(selected_word, language_name)
57
 
58
  try:
59
+ stream = client.chat.completions.create(
60
  messages=messages,
61
+ temperature=0.3,
62
+ top_p=0.7,
63
+ max_tokens=128,
64
+ stream=True
65
  )
66
+
67
+ output = ""
68
+ for chunk in stream:
69
+ if chunk.choices and chunk.choices[0].delta.content:
70
+ output += chunk.choices[0].delta.content
71
+ yield output
72
 
73
  except Exception as e:
74
+ yield f"❌ Error: {e}"
75
 
76
 
77
+ # Build Gradio UI
78
  with gr.Blocks() as demo:
79
+ gr.Markdown("## πŸ“– Quran Word Explorer (with DeepSeek-V3) β€” Streaming Enabled")
80
+
81
  with gr.Row():
82
+ word_input = gr.Dropdown(choices=word_options, label="πŸ”€ Select Quranic Word")
83
+ lang_input = gr.Dropdown(choices=language_options, label="🌐 Select Language")
84
+
85
+ run_btn = gr.Button("πŸ” Get Explanation")
86
+ output = gr.Textbox(label="πŸ“˜ Output", lines=20)
87
 
88
+ run_btn.click(fn=process, inputs=[word_input, lang_input], outputs=output, streaming=True)
89
 
90
+ demo.launch()