|
import gradio as gr |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForQuestionAnswering |
|
|
|
|
|
hf_api_token = "HF_API_KEY" |
|
|
|
|
|
model_name = "allenai/Molmo-7B-D-0924" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_api_token) |
|
model = AutoModelForQuestionAnswering.from_pretrained(model_name, use_auth_token=hf_api_token) |
|
|
|
def generate_questions(file_content): |
|
|
|
try: |
|
text = file_content.decode("utf-8") |
|
except Exception as e: |
|
return f"Error decoding file: {str(e)}", "" |
|
|
|
|
|
inputs = tokenizer(text, return_tensors="pt") |
|
outputs = model(**inputs) |
|
answers = tokenizer.decode(outputs.start_logits.argmax(), skip_special_tokens=True) |
|
|
|
|
|
questions = [] |
|
options = [] |
|
for answer in answers.split("."): |
|
if answer.startswith("Q"): |
|
questions.append(answer.strip()) |
|
else: |
|
options.append(answer.strip()) |
|
|
|
return questions, options |
|
|
|
|
|
question_box = gr.Textbox(label="Questions") |
|
option_box = gr.Textbox(label="Options") |
|
|
|
iface = gr.Interface( |
|
fn=generate_questions, |
|
inputs=gr.File(label="Upload File"), |
|
outputs=[question_box, option_box], |
|
title="Question and Option Generator" |
|
) |
|
|
|
iface.launch() |
|
|