Bofandra commited on
Commit
a3d1c69
·
verified ·
1 Parent(s): 76e7c7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -38
app.py CHANGED
@@ -1,10 +1,16 @@
1
- import gradio as gr
2
- import requests
3
  import os
4
- import requests
5
  import json
 
 
 
 
 
 
 
 
6
 
7
- # Gradio-compatible data loading (as before)
8
  with open("top_500_quran_lemmas_fixed.json", encoding="utf-8") as f:
9
  word_list = json.load(f)
10
 
@@ -14,55 +20,60 @@ with open("language_list.json", encoding="utf-8") as f:
14
  word_options = [f"{word['text']} ({word['english']})" for word in word_list]
15
  language_options = [f"{lang['name']} ({lang['code']})" for lang in language_list]
16
 
17
- # Use Hugging Face Inference API
18
- API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-V3"
19
- HEADERS = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
20
-
21
- def query_hf_model(payload):
22
- response = requests.post(API_URL, headers=HEADERS, json=payload)
23
- return response.json()
24
-
25
- def create_prompt(word_entry, language_code):
26
- return f"""
27
- You are a friendly Quranic AI assistant.
28
 
29
- The word is: {word_entry['text']} ({word_entry['english']}).
 
 
 
 
 
 
 
 
 
30
 
31
- Please provide the following in easy-to-understand {language_code}:
32
- 1. Translation of the word.
33
- 2. Its root word and any related derivatives.
34
- 3. Where it appears in the Qur'an (surah and ayah).
35
- 4. Simple explanation for each occurrence.
 
 
 
36
 
37
- Avoid technical terms. Think like a teacher explaining clearly to students.
38
- """.strip()
39
 
40
  def process(word_label, lang_label):
41
  selected_word = next((w for w in word_list if w['text'] in word_label), None)
42
  language_code = lang_label.split("(")[-1].strip(")")
 
43
  if not selected_word:
44
- return "Word not found."
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- prompt = create_prompt(selected_word, language_code)
47
- output = query_hf_model({"inputs": prompt})
48
 
49
- # Some formatting safety
50
- if isinstance(output, dict) and "error" in output:
51
- return f"Error: {output['error']}"
52
- elif isinstance(output, list):
53
- return output[0].get("generated_text", "No response.")
54
- else:
55
- return str(output)
56
 
57
  # Gradio UI
58
  with gr.Blocks() as demo:
59
- gr.Markdown("## 📖 Quran Word Explorer using DeepSeek-V3 (HF Inference API)")
60
  with gr.Row():
61
- word_input = gr.Dropdown(choices=word_options, label="Select a Quran Word")
62
  lang_input = gr.Dropdown(choices=language_options, label="Select Language")
63
-
64
- output = gr.Textbox(label="DeepSeek-V3 Output", lines=20)
65
  run_btn = gr.Button("Get Info")
 
 
66
  run_btn.click(fn=process, inputs=[word_input, lang_input], outputs=output)
67
 
68
- demo.launch()
 
 
 
1
  import os
2
+ 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
 
 
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",
28
+ "content": "You are a helpful and friendly assistant that explains Quranic words in a simple way.",
29
+ },
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
+ ]
43
 
 
 
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()