Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,78 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
api_token = os.getenv("API_TOKEN")
|
| 8 |
if not api_token:
|
| 9 |
-
raise ValueError("API token not found.
|
| 10 |
|
| 11 |
-
|
| 12 |
-
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3" # Corrected URL
|
| 13 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
{passage} [/INST] Exegesis:</s>"""
|
| 25 |
-
|
| 26 |
-
payload = {
|
| 27 |
-
"inputs": prompt,
|
| 28 |
-
}
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 32 |
response.raise_for_status()
|
| 33 |
result = response.json()
|
| 34 |
-
|
| 35 |
-
|
| 36 |
if isinstance(result, list) and len(result) > 0:
|
| 37 |
-
|
| 38 |
-
marker = "Exegesis:" # Marker to split on
|
| 39 |
-
if marker in generated_text:
|
| 40 |
-
# Return only the text after the marker
|
| 41 |
-
generated_text = generated_text.split(marker, 1)[1].strip()
|
| 42 |
-
return generated_text or "Error: No response from model."
|
| 43 |
else:
|
| 44 |
return "Error: Unexpected response format."
|
| 45 |
except requests.exceptions.RequestException as e:
|
| 46 |
return f"API Error: {e}"
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
description="Enter a Bible passage to receive insightful exegesis commentary using the Hugging Face API."
|
| 56 |
)
|
| 57 |
|
| 58 |
-
if __name__ == "__main__":
|
| 59 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import pytesseract
|
| 4 |
+
import pdfplumber
|
| 5 |
+
import docx
|
| 6 |
import requests
|
| 7 |
+
from PIL import Image
|
| 8 |
|
| 9 |
+
# API Configuration
|
| 10 |
api_token = os.getenv("API_TOKEN")
|
| 11 |
if not api_token:
|
| 12 |
+
raise ValueError("API token not found. Set 'API_TOKEN' in your environment variables.")
|
| 13 |
|
| 14 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
|
|
|
| 15 |
HEADERS = {"Authorization": f"Bearer {api_token}"}
|
| 16 |
|
| 17 |
+
def extract_text_from_pdf(pdf_file):
|
| 18 |
+
text = ""
|
| 19 |
+
with pdfplumber.open(pdf_file) as pdf:
|
| 20 |
+
for page in pdf.pages:
|
| 21 |
+
text += page.extract_text() + "\n"
|
| 22 |
+
return text.strip()
|
| 23 |
|
| 24 |
+
def extract_text_from_docx(docx_file):
|
| 25 |
+
doc = docx.Document(docx_file)
|
| 26 |
+
return "\n".join([para.text for para in doc.paragraphs])
|
| 27 |
|
| 28 |
+
def extract_text_from_image(image_file):
|
| 29 |
+
image = Image.open(image_file)
|
| 30 |
+
return pytesseract.image_to_string(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def generate_quiz(text):
|
| 33 |
+
if not text.strip():
|
| 34 |
+
return "No content extracted. Please upload a valid file."
|
| 35 |
+
|
| 36 |
+
prompt = f"""<s>[INST] Generate a quiz with multiple-choice questions based on the following content:
|
| 37 |
+
{text} [/INST] Quiz:</s>"""
|
| 38 |
+
|
| 39 |
+
payload = {"inputs": prompt}
|
| 40 |
+
|
| 41 |
try:
|
| 42 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 43 |
response.raise_for_status()
|
| 44 |
result = response.json()
|
| 45 |
+
|
|
|
|
| 46 |
if isinstance(result, list) and len(result) > 0:
|
| 47 |
+
return result[0].get("generated_text", "Quiz generation failed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
else:
|
| 49 |
return "Error: Unexpected response format."
|
| 50 |
except requests.exceptions.RequestException as e:
|
| 51 |
return f"API Error: {e}"
|
| 52 |
|
| 53 |
+
def quiz_app(uploaded_file):
|
| 54 |
+
file_type = uploaded_file.name.split('.')[-1]
|
| 55 |
+
extracted_text = ""
|
| 56 |
+
|
| 57 |
+
if file_type == "pdf":
|
| 58 |
+
extracted_text = extract_text_from_pdf(uploaded_file)
|
| 59 |
+
elif file_type == "docx":
|
| 60 |
+
extracted_text = extract_text_from_docx(uploaded_file)
|
| 61 |
+
elif file_type in ["png", "jpg", "jpeg"]:
|
| 62 |
+
extracted_text = extract_text_from_image(uploaded_file)
|
| 63 |
+
|
| 64 |
+
if not extracted_text:
|
| 65 |
+
return "No text extracted. Please upload a valid file."
|
| 66 |
+
|
| 67 |
+
return generate_quiz(extracted_text)
|
| 68 |
|
| 69 |
+
iface = gr.Interface(
|
| 70 |
+
fn=quiz_app,
|
| 71 |
+
inputs=gr.File(label="Upload PDF, DOCX, or Image"),
|
| 72 |
+
outputs=gr.Textbox(label="Generated Quiz"),
|
| 73 |
+
title="AI Quiz Generator",
|
| 74 |
+
description="Upload a file to generate a quiz based on its content."
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
iface.launch()
|