pvyas96 commited on
Commit
73cb37e
1 Parent(s): e7e6fc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -1,21 +1,14 @@
 
 
1
  import PyPDF2
2
- import torch
3
  import gradio as gr
4
 
5
- # Make sure the model path is correct for your system!
6
- llm = LlamaCpp(
7
- model_path="./llama-2-7b-chat.Q4_K_S.gguf",
8
- temperature=0.2,
9
- n_ctx=512,
10
- max_tokens=2000,
11
- top_p=1,
12
- )
13
 
14
- template = """Generate only one MCQ question based on text \
15
- that is delimited by triple backticks \
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
- input_msg = PromptTemplate.from_template(template=template)
53
- input_s = input_msg.format(pattern=pattern, text=para)
54
- output_msg = llm(input_s)
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  output_file = "questions.txt"
56
  with open(output_file, "w") as f:
57
- f.write(output_msg)
58
- return output_msg, output_file
 
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(