Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import json | |
def read_questions(file_path): | |
questions = [] | |
with open(file_path, 'r') as file: | |
lines = file.readlines() | |
question = '' | |
options = [] | |
for line in lines: | |
if line.startswith('##'): | |
if question: | |
questions.append({'question': question, 'options': options}) | |
question = '' | |
options = [] | |
question = line[2:].strip() | |
elif line.startswith('**'): | |
options.append(line[2:].strip()) | |
if question: | |
questions.append({'question': question, 'options': options}) | |
return questions | |
def send_questions_to_telegram(questions): | |
base_url = "https://api.telegram.org/bot6376553042:AAEWlIVLhWwGrudAiry6LrzRL3yh-nzl-oE/sendPoll" | |
chat_id = "-4010995712" | |
for q in questions: | |
parameters = { | |
"chat_id": chat_id, | |
"question": q['question'], | |
"options": json.dumps(q['options']), | |
"type": "quiz", | |
"correct_option_id": 0 | |
} | |
response = requests.post(base_url, data=parameters) | |
print(response.json()) | |
def process_questions(file_path): | |
questions = read_questions(file_path) | |
send_questions_to_telegram(questions) | |
def get_file_path_and_process(file): | |
file_path = file.name | |
process_questions(file_path) | |
iface = gr.Interface( | |
fn=get_file_path_and_process, | |
inputs=gr.File(label="Upload Questions File"), | |
outputs="text", | |
title="Send Quiz Questions to Telegram", | |
description="Upload a text file with questions formatted with '##' for questions and '**' for options to send them as polls to a Telegram chat." | |
) | |
iface.launch() |