Josebert commited on
Commit
25d3696
·
verified ·
1 Parent(s): 29439ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -31
app.py CHANGED
@@ -3,68 +3,62 @@ import gradio as gr
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(
61
- label="Enter Bible Verse",
62
- placeholder="e.g., 'I am the vine, you are the branches' - John 15:5",
63
- lines=3
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
 
 
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
+ # Use the token in request headers
12
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
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
  prompt = f"""
20
+ You are JR-Sacred Syntax, an expert biblical scholar. Analyze the given Bible verse and determine if it contains a figure of speech such as Synecdoche, Metonymy, Hyperbole, Simile, or Paradox.
21
+ Return the response in JSON format with the following structure:
 
22
  {{
23
+ "verse": "[Provide the Bible verse]",
24
  "figure": "[Identify the figure of speech]",
25
+ "explanation": "[Clear explanation]",
26
  "examples": [
27
  {{
28
+ "phrase": "[Phrase from verse]",
29
+ "analysis": "[Explanation]"
30
  }}
31
  ]
32
  }}
33
+ Now, analyze this verse:
34
  "{verse}"
 
35
  """
36
 
37
  payload = {"inputs": prompt}
38
+
39
  try:
40
  response = requests.post(API_URL, headers=HEADERS, json=payload)
41
  response.raise_for_status()
42
  result = response.json()
43
 
44
+ if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
45
+ generated_text = result[0]["generated_text"].strip()
 
 
 
46
  try:
47
+ return json.dumps(json.loads(generated_text), indent=2)
 
48
  except json.JSONDecodeError:
49
+ return "Error: Could not parse API response. The response might not be in the expected JSON format."
50
  else:
51
+ return "Error: Unexpected response format from API."
52
  except requests.exceptions.RequestException as e:
53
  return f"API Error: {e}"
54
 
55
+ # Define Gradio UI
56
  demo = gr.Interface(
57
  fn=analyze_figure_of_speech,
58
+ inputs=gr.Textbox(label="Enter Bible Verse", placeholder="e.g., 'I am the vine, you are the branches' - John 15:5", lines=3),
 
 
 
 
59
  outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"),
60
  title="JR-Sacred Syntax: Bible Figures of Speech Detector",
61
+ description="Enter a Bible verse to detect and analyze figures of speech with detailed explanations.",
62
  allow_flagging="never"
63
  )
64