Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,14 @@
|
|
|
|
|
|
1 |
import PyPDF2
|
2 |
-
import torch
|
3 |
import gradio as gr
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
temperature=0.2,
|
9 |
-
n_ctx=512,
|
10 |
-
max_tokens=2000,
|
11 |
-
top_p=1,
|
12 |
-
)
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
with {pattern} pattern. \
|
17 |
-
text: `{text}` \
|
18 |
-
"""
|
19 |
|
20 |
|
21 |
def extract_paragraphs(pdf_file):
|
@@ -49,13 +42,27 @@ def extract_paragraphs(pdf_file):
|
|
49 |
def Generate_mcq_from_pdf(pdf_file):
|
50 |
paragraphs = extract_paragraphs(pdf_file)
|
51 |
for para in paragraphs:
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
output_file = "questions.txt"
|
56 |
with open(output_file, "w") as f:
|
57 |
-
f.write(
|
58 |
-
|
|
|
59 |
|
60 |
|
61 |
app = gr.Interface(
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
2 |
+
import requests
|
3 |
import PyPDF2
|
|
|
4 |
import gradio as gr
|
5 |
|
6 |
+
# Replace with your Hugging Face API token
|
7 |
+
api_token = "YOUR_HUGGING_FACE_TOKEN"
|
8 |
+
mistral_model_id = "NousResearch/Hermes-2-Pro-Mistral-7B" # Choose appropriate model version
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(mistral_model_id)
|
11 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(mistral_model_id)
|
|
|
|
|
|
|
12 |
|
13 |
|
14 |
def extract_paragraphs(pdf_file):
|
|
|
42 |
def Generate_mcq_from_pdf(pdf_file):
|
43 |
paragraphs = extract_paragraphs(pdf_file)
|
44 |
for para in paragraphs:
|
45 |
+
template = """Generate only one MCQ question based on text \
|
46 |
+
that is delimited by triple backticks \
|
47 |
+
with {pattern} pattern. \
|
48 |
+
text: `{text}` \
|
49 |
+
"""
|
50 |
+
prompt = template.format(pattern="IIT GATE", text=para)
|
51 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
52 |
+
|
53 |
+
headers = {"Authorization": f"Bearer {api_token}"}
|
54 |
+
url = f"https://api-inference.huggingface.co/models/{mistral_model_id}"
|
55 |
+
response = requests.post(url, headers=headers, json=inputs)
|
56 |
+
response.raise_for_status() # Raise an error if request fails
|
57 |
+
|
58 |
+
output_ids = response.json()["generated_ids"]
|
59 |
+
output_text = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
|
60 |
+
|
61 |
output_file = "questions.txt"
|
62 |
with open(output_file, "w") as f:
|
63 |
+
f.write(output_text)
|
64 |
+
|
65 |
+
return output_text, output_file
|
66 |
|
67 |
|
68 |
app = gr.Interface(
|