Josebert commited on
Commit
29439ca
·
verified ·
1 Parent(s): 39d5100

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -3,58 +3,58 @@ import gradio as gr
3
  import requests
4
  import json
5
 
6
- # Retrieve the API token from environment variables
7
- api_token = os.getenv("API_TOKEN")
8
- if not api_token:
9
- raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the environment variables.")
10
-
11
- # API configuration
12
- API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
13
- HEADERS = {"Authorization": f"Bearer {api_token}"}
14
-
15
  def analyze_figure_of_speech(verse):
 
 
 
 
 
 
 
16
  if not verse.strip():
17
  return "Please enter a Bible verse."
18
 
19
- # Construct a prompt to instruct the model
20
  prompt = f"""
21
- [INST] You are JR-Sacred Syntax, an expert biblical scholar specializing in Bible figures of speech.
22
- Identify the literary device in the following verse (e.g., Synecdoche, Metonymy, Hyperbole, Simile, Paradox) and structure the response as JSON:
23
-
24
  {{
25
- "verse": "{verse}",
26
  "figure": "[Identify the figure of speech]",
27
- "explanation": "[Give a detailed yet concise explanation]",
28
  "examples": [
29
  {{
30
- "phrase": "[Highlight the specific phrase]",
31
- "analysis": "[Explain why this phrase fits the identified figure of speech]"
32
  }}
33
  ]
34
  }}
35
- [/INST]
 
 
36
  """
37
 
38
- payload = {"inputs": prompt, "parameters": {"max_new_tokens": 250, "temperature": 0.7, "top_p": 0.9}}
39
-
40
  try:
41
  response = requests.post(API_URL, headers=HEADERS, json=payload)
42
  response.raise_for_status()
43
  result = response.json()
44
 
45
- if isinstance(result, list) and result:
 
 
 
46
  generated_text = result[0].get("generated_text", "")
47
  try:
48
  output_json = json.loads(generated_text)
49
  return json.dumps(output_json, indent=2)
50
  except json.JSONDecodeError:
51
- return "Error processing response. Please try again."
52
  else:
53
- return "Unexpected response format."
54
  except requests.exceptions.RequestException as e:
55
  return f"API Error: {e}"
56
 
57
- # Gradio Interface
58
  demo = gr.Interface(
59
  fn=analyze_figure_of_speech,
60
  inputs=gr.Textbox(
@@ -64,8 +64,8 @@ demo = gr.Interface(
64
  ),
65
  outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"),
66
  title="JR-Sacred Syntax: Bible Figures of Speech Detector",
67
- description="Enter a Bible verse to detect and analyze figures of speech with detailed explanations.",
68
- live=False # Prevents automatic execution
69
  )
70
 
71
  if __name__ == "__main__":
 
3
  import requests
4
  import json
5
 
 
 
 
 
 
 
 
 
 
6
  def analyze_figure_of_speech(verse):
7
+ api_token = os.getenv("API_TOKEN")
8
+ if not api_token:
9
+ return "Error: API token is missing. Please set 'API_TOKEN' in the environment."
10
+
11
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
12
+ HEADERS = {"Authorization": f"Bearer {api_token}"}
13
+
14
  if not verse.strip():
15
  return "Please enter a Bible verse."
16
 
 
17
  prompt = f"""
18
+ <s>[INST] You are JR-Sacred Syntax, an expert biblical scholar specializing in the analysis of Bible verses.
19
+ Your task is to analyze the given Bible verse and determine if it contains a figure of speech such as Synecdoche, Metonymy, Hyperbole, Simile, or Paradox.
20
+ Follow this structured output format:
21
  {{
22
+ "verse": "[Provide the NKJV Bible verse]",
23
  "figure": "[Identify the figure of speech]",
24
+ "explanation": "[Give a clear explanation]",
25
  "examples": [
26
  {{
27
+ "phrase": "[Identify the phrase from the verse]",
28
+ "analysis": "[Explain why this phrase qualifies as the identified figure of speech]"
29
  }}
30
  ]
31
  }}
32
+ Now, analyze the following Bible verse:
33
+ "{verse}"
34
+ [/INST]</s>
35
  """
36
 
37
+ payload = {"inputs": prompt}
 
38
  try:
39
  response = requests.post(API_URL, headers=HEADERS, json=payload)
40
  response.raise_for_status()
41
  result = response.json()
42
 
43
+ if isinstance(result, dict) and "error" in result:
44
+ return f"API Error: {result['error']}"
45
+
46
+ if isinstance(result, list) and len(result) > 0:
47
  generated_text = result[0].get("generated_text", "")
48
  try:
49
  output_json = json.loads(generated_text)
50
  return json.dumps(output_json, indent=2)
51
  except json.JSONDecodeError:
52
+ return "Error: Could not parse API response."
53
  else:
54
+ return "Error: Unexpected response format."
55
  except requests.exceptions.RequestException as e:
56
  return f"API Error: {e}"
57
 
 
58
  demo = gr.Interface(
59
  fn=analyze_figure_of_speech,
60
  inputs=gr.Textbox(
 
64
  ),
65
  outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"),
66
  title="JR-Sacred Syntax: Bible Figures of Speech Detector",
67
+ description="Enter a Bible verse to detect and analyze figures of speech with detailed biblical explanations.",
68
+ allow_flagging="never"
69
  )
70
 
71
  if __name__ == "__main__":