Josebert commited on
Commit
5b14ccc
·
verified ·
1 Parent(s): 25d3696

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -3,12 +3,12 @@ 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
- # 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
 
@@ -16,51 +16,58 @@ 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
 
65
  if __name__ == "__main__":
66
- demo.launch()
 
3
  import requests
4
  import json
5
 
6
+ # Retrieve the API token from secrets
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 Secrets.")
10
 
11
+ # Use the token in your 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
 
 
16
  if not verse.strip():
17
  return "Please enter a Bible verse."
18
 
19
+ # Construct a prompt to instruct the model to analyze figures of speech in the verse.
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 len(result) > 0:
46
+ generated_text = result[0].get("generated_text", "")
47
+ marker = "Figures:" # Marker to help extract the output
48
+ if marker in generated_text:
49
+ generated_text = generated_text.split(marker, 1)[1].strip()
50
  try:
51
+ output_json = json.loads(generated_text)
52
+ return json.dumps(output_json, indent=2)
53
  except json.JSONDecodeError:
54
+ return generated_text
55
  else:
56
+ return "Error: Unexpected response format."
57
  except requests.exceptions.RequestException as e:
58
  return f"API Error: {e}"
59
 
 
60
  demo = gr.Interface(
61
  fn=analyze_figure_of_speech,
62
+ inputs=gr.Textbox(
63
+ label="Enter Bible Verse",
64
+ placeholder="e.g., 'I am the vine, you are the branches' - John 15:5",
65
+ lines=3
66
+ ),
67
  outputs=gr.Textbox(label="Figures of Speech Analysis (JSON)"),
68
  title="JR-Sacred Syntax: Bible Figures of Speech Detector",
69
+ description="Enter a Bible verse to detect and analyze figures of speech with detailed biblical explanations."
 
70
  )
71
 
72
  if __name__ == "__main__":
73
+ demo.launch()