Josebert commited on
Commit
67a2841
·
verified ·
1 Parent(s): 3692709

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -34
app.py CHANGED
@@ -10,7 +10,7 @@ if not api_token:
10
  raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the Secrets.")
11
 
12
  # Use the token in your request headers
13
- API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.3-70B-Instruct" # Corrected URL
14
  HEADERS = {"Authorization": f"Bearer {api_token}"}
15
 
16
  # Cache to store API responses for 10 minutes
@@ -26,21 +26,17 @@ def generate_exegesis(passage):
26
 
27
  {passage} [/INST] Exegesis:</s>"""
28
 
29
- payload = {
30
- "inputs": prompt,
31
- }
32
 
33
  try:
34
  response = requests.post(API_URL, headers=HEADERS, json=payload)
35
  response.raise_for_status()
36
  result = response.json()
37
- print("Full API Response:", json.dumps(result, indent=4)) # Debug output
38
 
39
  if isinstance(result, list) and len(result) > 0:
40
  generated_text = result[0].get("generated_text", "")
41
  marker = "Exegesis:" # Marker to split on
42
  if marker in generated_text:
43
- # Return only the text after the marker
44
  generated_text = generated_text.split(marker, 1)[1].strip()
45
  return generated_text or "Error: No response from model."
46
  else:
@@ -57,21 +53,17 @@ def answer_question(question):
57
 
58
  {question} [/INST] Answer:</s>"""
59
 
60
- payload = {
61
- "inputs": prompt,
62
- }
63
 
64
  try:
65
  response = requests.post(API_URL, headers=HEADERS, json=payload)
66
  response.raise_for_status()
67
  result = response.json()
68
- print("Full API Response:", json.dumps(result, indent=4)) # Debug output
69
 
70
  if isinstance(result, list) and len(result) > 0:
71
  generated_text = result[0].get("generated_text", "")
72
  marker = "Answer:" # Marker to split on
73
  if marker in generated_text:
74
- # Return only the text after the marker
75
  generated_text = generated_text.split(marker, 1)[1].strip()
76
  return generated_text or "Error: No response from model."
77
  else:
@@ -88,21 +80,17 @@ def create_sermon(topic):
88
 
89
  {topic} [/INST] Sermon Outline:</s>"""
90
 
91
- payload = {
92
- "inputs": prompt,
93
- }
94
 
95
  try:
96
  response = requests.post(API_URL, headers=HEADERS, json=payload)
97
  response.raise_for_status()
98
  result = response.json()
99
- print("Full API Response:", json.dumps(result, indent=4)) # Debug output
100
 
101
  if isinstance(result, list) and len(result) > 0:
102
  generated_text = result[0].get("generated_text", "")
103
  marker = "Sermon Outline:" # Marker to split on
104
  if marker in generated_text:
105
- # Return only the text after the marker
106
  generated_text = generated_text.split(marker, 1)[1].strip()
107
  return generated_text or "Error: No response from model."
108
  else:
@@ -110,24 +98,25 @@ def create_sermon(topic):
110
  except requests.exceptions.RequestException as e:
111
  return f"API Error: {e}"
112
 
113
- # Gradio interface
114
- demo = gr.Interface(
115
- fn=[generate_exegesis, answer_question, create_sermon],
116
- inputs=[
117
- gr.Textbox(label="Enter Bible Passage", placeholder="e.g., John 3:16"),
118
- gr.Textbox(label="Ask a Question", placeholder="e.g., What is the meaning of life?"),
119
- gr.Textbox(label="Enter Sermon Topic", placeholder="e.g., Faith")
120
- ],
121
- outputs=[
122
- gr.Textbox(label="Exegesis Commentary"),
123
- gr.Textbox(label="Answer"),
124
- gr.Textbox(label="Sermon Outline")
125
- ],
126
- title="JR Study Bible",
127
- description="A multifunctional Bible study group app that performs exegesis, answers questions, and helps create sermon outlines.",
128
- theme="huggingface",
129
- live=True
130
- )
 
131
 
132
  if __name__ == "__main__":
133
  demo.launch()
 
10
  raise ValueError("API token not found. Make sure 'API_TOKEN' is set in the Secrets.")
11
 
12
  # Use the token in your request headers
13
+ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.3-70B-Instruct"
14
  HEADERS = {"Authorization": f"Bearer {api_token}"}
15
 
16
  # Cache to store API responses for 10 minutes
 
26
 
27
  {passage} [/INST] Exegesis:</s>"""
28
 
29
+ payload = {"inputs": prompt}
 
 
30
 
31
  try:
32
  response = requests.post(API_URL, headers=HEADERS, json=payload)
33
  response.raise_for_status()
34
  result = response.json()
 
35
 
36
  if isinstance(result, list) and len(result) > 0:
37
  generated_text = result[0].get("generated_text", "")
38
  marker = "Exegesis:" # Marker to split on
39
  if marker in generated_text:
 
40
  generated_text = generated_text.split(marker, 1)[1].strip()
41
  return generated_text or "Error: No response from model."
42
  else:
 
53
 
54
  {question} [/INST] Answer:</s>"""
55
 
56
+ payload = {"inputs": prompt}
 
 
57
 
58
  try:
59
  response = requests.post(API_URL, headers=HEADERS, json=payload)
60
  response.raise_for_status()
61
  result = response.json()
 
62
 
63
  if isinstance(result, list) and len(result) > 0:
64
  generated_text = result[0].get("generated_text", "")
65
  marker = "Answer:" # Marker to split on
66
  if marker in generated_text:
 
67
  generated_text = generated_text.split(marker, 1)[1].strip()
68
  return generated_text or "Error: No response from model."
69
  else:
 
80
 
81
  {topic} [/INST] Sermon Outline:</s>"""
82
 
83
+ payload = {"inputs": prompt}
 
 
84
 
85
  try:
86
  response = requests.post(API_URL, headers=HEADERS, json=payload)
87
  response.raise_for_status()
88
  result = response.json()
 
89
 
90
  if isinstance(result, list) and len(result) > 0:
91
  generated_text = result[0].get("generated_text", "")
92
  marker = "Sermon Outline:" # Marker to split on
93
  if marker in generated_text:
 
94
  generated_text = generated_text.split(marker, 1)[1].strip()
95
  return generated_text or "Error: No response from model."
96
  else:
 
98
  except requests.exceptions.RequestException as e:
99
  return f"API Error: {e}"
100
 
101
+ # Gradio Interface with Tabs
102
+ with gr.Blocks(theme="huggingface") as demo:
103
+ gr.Markdown("# JR Study Bible")
104
+ gr.Markdown("A multifunctional Bible study group app that performs exegesis, answers questions, and helps create sermon outlines.")
105
+
106
+ with gr.Tab("Exegesis"):
107
+ passage_input = gr.Textbox(label="Enter Bible Passage", placeholder="e.g., John 3:16")
108
+ exegesis_output = gr.Textbox(label="Exegesis Commentary")
109
+ passage_input.change(generate_exegesis, inputs=passage_input, outputs=exegesis_output)
110
+
111
+ with gr.Tab("Question Answering"):
112
+ question_input = gr.Textbox(label="Ask a Question", placeholder="e.g., What is the meaning of life?")
113
+ question_output = gr.Textbox(label="Answer")
114
+ question_input.change(answer_question, inputs=question_input, outputs=question_output)
115
+
116
+ with gr.Tab("Sermon Creation"):
117
+ topic_input = gr.Textbox(label="Enter Sermon Topic", placeholder="e.g., Faith")
118
+ sermon_output = gr.Textbox(label="Sermon Outline")
119
+ topic_input.change(create_sermon, inputs=topic_input, outputs=sermon_output)
120
 
121
  if __name__ == "__main__":
122
  demo.launch()