prithivMLmods commited on
Commit
eef4f2d
·
verified ·
1 Parent(s): d7da47f

Delete open-r1-reasoner-doc-py/open-r1-exp.ipynb

Browse files
open-r1-reasoner-doc-py/open-r1-exp.ipynb DELETED
@@ -1,343 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "markdown",
5
- "metadata": {
6
- "id": "-b4-SW1aGOcF"
7
- },
8
- "source": [
9
- "# **Open R1 Reasoning Exp**\n",
10
- "\n",
11
- "Qwen2VLForConditionalGeneration"
12
- ]
13
- },
14
- {
15
- "cell_type": "code",
16
- "execution_count": null,
17
- "metadata": {
18
- "id": "oDmd1ZObGSel"
19
- },
20
- "outputs": [],
21
- "source": [
22
- "!pip install gradio spaces transformers accelerate numpy requests torch torchvision qwen-vl-utils av ipython reportlab fpdf python-docx pillow huggingface_hub"
23
- ]
24
- },
25
- {
26
- "cell_type": "code",
27
- "execution_count": null,
28
- "metadata": {
29
- "id": "ovBSsRFhGbs2"
30
- },
31
- "outputs": [],
32
- "source": [
33
- "# Authenticate with Hugging Face\n",
34
- "from huggingface_hub import login\n",
35
- "\n",
36
- "# Log in to Hugging Face using the provided token\n",
37
- "hf_token = '---xxxxx---'\n",
38
- "login(hf_token)\n",
39
- "\n",
40
- "#Demo\n",
41
- "import gradio as gr\n",
42
- "import spaces\n",
43
- "from transformers import Qwen2VLForConditionalGeneration, AutoProcessor, TextIteratorStreamer\n",
44
- "from qwen_vl_utils import process_vision_info\n",
45
- "import torch\n",
46
- "from PIL import Image\n",
47
- "import os\n",
48
- "import uuid\n",
49
- "import io\n",
50
- "from threading import Thread\n",
51
- "from reportlab.lib.pagesizes import A4\n",
52
- "from reportlab.lib.styles import getSampleStyleSheet\n",
53
- "from reportlab.lib import colors\n",
54
- "from reportlab.platypus import SimpleDocTemplate, Image as RLImage, Paragraph, Spacer\n",
55
- "from reportlab.lib.units import inch\n",
56
- "from reportlab.pdfbase import pdfmetrics\n",
57
- "from reportlab.pdfbase.ttfonts import TTFont\n",
58
- "import docx\n",
59
- "from docx.enum.text import WD_ALIGN_PARAGRAPH\n",
60
- "\n",
61
- "# Define model options\n",
62
- "MODEL_OPTIONS = {\n",
63
- " \"OpenR1\": \"prithivMLmods/Open-R1-Mini-Experimental\",\n",
64
- "}\n",
65
- "\n",
66
- "# Preload models and processors into CUDA\n",
67
- "models = {}\n",
68
- "processors = {}\n",
69
- "for name, model_id in MODEL_OPTIONS.items():\n",
70
- " print(f\"Loading {name}...\")\n",
71
- " models[name] = Qwen2VLForConditionalGeneration.from_pretrained(\n",
72
- " model_id,\n",
73
- " trust_remote_code=True,\n",
74
- " torch_dtype=torch.float16\n",
75
- " ).to(\"cuda\").eval()\n",
76
- " processors[name] = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)\n",
77
- "\n",
78
- "image_extensions = Image.registered_extensions()\n",
79
- "\n",
80
- "def identify_and_save_blob(blob_path):\n",
81
- " \"\"\"Identifies if the blob is an image and saves it.\"\"\"\n",
82
- " try:\n",
83
- " with open(blob_path, 'rb') as file:\n",
84
- " blob_content = file.read()\n",
85
- " try:\n",
86
- " Image.open(io.BytesIO(blob_content)).verify() # Check if it's a valid image\n",
87
- " extension = \".png\" # Default to PNG for saving\n",
88
- " media_type = \"image\"\n",
89
- " except (IOError, SyntaxError):\n",
90
- " raise ValueError(\"Unsupported media type. Please upload a valid image.\")\n",
91
- "\n",
92
- " filename = f\"temp_{uuid.uuid4()}_media{extension}\"\n",
93
- " with open(filename, \"wb\") as f:\n",
94
- " f.write(blob_content)\n",
95
- "\n",
96
- " return filename, media_type\n",
97
- "\n",
98
- " except FileNotFoundError:\n",
99
- " raise ValueError(f\"The file {blob_path} was not found.\")\n",
100
- " except Exception as e:\n",
101
- " raise ValueError(f\"An error occurred while processing the file: {e}\")\n",
102
- "\n",
103
- "@spaces.GPU\n",
104
- "def qwen_inference(model_name, media_input, text_input=None):\n",
105
- " \"\"\"Handles inference for the selected model.\"\"\"\n",
106
- " model = models[model_name]\n",
107
- " processor = processors[model_name]\n",
108
- "\n",
109
- " if isinstance(media_input, str):\n",
110
- " media_path = media_input\n",
111
- " if media_path.endswith(tuple([i for i in image_extensions.keys()])):\n",
112
- " media_type = \"image\"\n",
113
- " else:\n",
114
- " try:\n",
115
- " media_path, media_type = identify_and_save_blob(media_input)\n",
116
- " except Exception as e:\n",
117
- " raise ValueError(\"Unsupported media type. Please upload a valid image.\")\n",
118
- "\n",
119
- " messages = [\n",
120
- " {\n",
121
- " \"role\": \"user\",\n",
122
- " \"content\": [\n",
123
- " {\n",
124
- " \"type\": media_type,\n",
125
- " media_type: media_path\n",
126
- " },\n",
127
- " {\"type\": \"text\", \"text\": text_input},\n",
128
- " ],\n",
129
- " }\n",
130
- " ]\n",
131
- "\n",
132
- " text = processor.apply_chat_template(\n",
133
- " messages, tokenize=False, add_generation_prompt=True\n",
134
- " )\n",
135
- " image_inputs, _ = process_vision_info(messages)\n",
136
- " inputs = processor(\n",
137
- " text=[text],\n",
138
- " images=image_inputs,\n",
139
- " padding=True,\n",
140
- " return_tensors=\"pt\",\n",
141
- " ).to(\"cuda\")\n",
142
- "\n",
143
- " streamer = TextIteratorStreamer(\n",
144
- " processor.tokenizer, skip_prompt=True, skip_special_tokens=True\n",
145
- " )\n",
146
- " generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)\n",
147
- "\n",
148
- " thread = Thread(target=model.generate, kwargs=generation_kwargs)\n",
149
- " thread.start()\n",
150
- "\n",
151
- " buffer = \"\"\n",
152
- " for new_text in streamer:\n",
153
- " buffer += new_text\n",
154
- " # Remove <|im_end|> or similar tokens from the output\n",
155
- " buffer = buffer.replace(\"<|im_end|>\", \"\")\n",
156
- " yield buffer\n",
157
- "\n",
158
- "def format_plain_text(output_text):\n",
159
- " \"\"\"Formats the output text as plain text without LaTeX delimiters.\"\"\"\n",
160
- " # Remove LaTeX delimiters and convert to plain text\n",
161
- " plain_text = output_text.replace(\"\\\\(\", \"\").replace(\"\\\\)\", \"\").replace(\"\\\\[\", \"\").replace(\"\\\\]\", \"\")\n",
162
- " return plain_text\n",
163
- "\n",
164
- "def generate_document(media_path, output_text, file_format, font_size, line_spacing, alignment, image_size):\n",
165
- " \"\"\"Generates a document with the input image and plain text output.\"\"\"\n",
166
- " plain_text = format_plain_text(output_text)\n",
167
- " if file_format == \"pdf\":\n",
168
- " return generate_pdf(media_path, plain_text, font_size, line_spacing, alignment, image_size)\n",
169
- " elif file_format == \"docx\":\n",
170
- " return generate_docx(media_path, plain_text, font_size, line_spacing, alignment, image_size)\n",
171
- "\n",
172
- "def generate_pdf(media_path, plain_text, font_size, line_spacing, alignment, image_size):\n",
173
- " \"\"\"Generates a PDF document.\"\"\"\n",
174
- " filename = f\"output_{uuid.uuid4()}.pdf\"\n",
175
- " doc = SimpleDocTemplate(\n",
176
- " filename,\n",
177
- " pagesize=A4,\n",
178
- " rightMargin=inch,\n",
179
- " leftMargin=inch,\n",
180
- " topMargin=inch,\n",
181
- " bottomMargin=inch\n",
182
- " )\n",
183
- " styles = getSampleStyleSheet()\n",
184
- " styles[\"Normal\"].fontSize = int(font_size)\n",
185
- " styles[\"Normal\"].leading = int(font_size) * line_spacing\n",
186
- " styles[\"Normal\"].alignment = {\n",
187
- " \"Left\": 0,\n",
188
- " \"Center\": 1,\n",
189
- " \"Right\": 2,\n",
190
- " \"Justified\": 4\n",
191
- " }[alignment]\n",
192
- "\n",
193
- " story = []\n",
194
- "\n",
195
- " # Add image with size adjustment\n",
196
- " image_sizes = {\n",
197
- " \"Small\": (200, 200),\n",
198
- " \"Medium\": (400, 400),\n",
199
- " \"Large\": (600, 600)\n",
200
- " }\n",
201
- " img = RLImage(media_path, width=image_sizes[image_size][0], height=image_sizes[image_size][1])\n",
202
- " story.append(img)\n",
203
- " story.append(Spacer(1, 12))\n",
204
- "\n",
205
- " # Add plain text output\n",
206
- " text = Paragraph(plain_text, styles[\"Normal\"])\n",
207
- " story.append(text)\n",
208
- "\n",
209
- " doc.build(story)\n",
210
- " return filename\n",
211
- "\n",
212
- "def generate_docx(media_path, plain_text, font_size, line_spacing, alignment, image_size):\n",
213
- " \"\"\"Generates a DOCX document.\"\"\"\n",
214
- " filename = f\"output_{uuid.uuid4()}.docx\"\n",
215
- " doc = docx.Document()\n",
216
- "\n",
217
- " # Add image with size adjustment\n",
218
- " image_sizes = {\n",
219
- " \"Small\": docx.shared.Inches(2),\n",
220
- " \"Medium\": docx.shared.Inches(4),\n",
221
- " \"Large\": docx.shared.Inches(6)\n",
222
- " }\n",
223
- " doc.add_picture(media_path, width=image_sizes[image_size])\n",
224
- " doc.add_paragraph()\n",
225
- "\n",
226
- " # Add plain text output\n",
227
- " paragraph = doc.add_paragraph()\n",
228
- " paragraph.paragraph_format.line_spacing = line_spacing\n",
229
- " paragraph.paragraph_format.alignment = {\n",
230
- " \"Left\": WD_ALIGN_PARAGRAPH.LEFT,\n",
231
- " \"Center\": WD_ALIGN_PARAGRAPH.CENTER,\n",
232
- " \"Right\": WD_ALIGN_PARAGRAPH.RIGHT,\n",
233
- " \"Justified\": WD_ALIGN_PARAGRAPH.JUSTIFY\n",
234
- " }[alignment]\n",
235
- " run = paragraph.add_run(plain_text)\n",
236
- " run.font.size = docx.shared.Pt(int(font_size))\n",
237
- "\n",
238
- " doc.save(filename)\n",
239
- " return filename\n",
240
- "\n",
241
- "# CSS for output styling\n",
242
- "css = \"\"\"\n",
243
- " #output {\n",
244
- " height: 500px;\n",
245
- " overflow: auto;\n",
246
- " border: 1px solid #ccc;\n",
247
- " }\n",
248
- ".submit-btn {\n",
249
- " background-color: #cf3434 !important;\n",
250
- " color: white !important;\n",
251
- "}\n",
252
- ".submit-btn:hover {\n",
253
- " background-color: #ff2323 !important;\n",
254
- "}\n",
255
- ".download-btn {\n",
256
- " background-color: #35a6d6 !important;\n",
257
- " color: white !important;\n",
258
- "}\n",
259
- ".download-btn:hover {\n",
260
- " background-color: #22bcff !important;\n",
261
- "}\n",
262
- "\"\"\"\n",
263
- "\n",
264
- "# Gradio app setup\n",
265
- "with gr.Blocks(css=css) as demo:\n",
266
- " gr.Markdown(\"# ChemQwen Chemical Identifier\")\n",
267
- "\n",
268
- " with gr.Tab(label=\"Image Input\"):\n",
269
- "\n",
270
- " with gr.Row():\n",
271
- " with gr.Column():\n",
272
- " model_choice = gr.Dropdown(\n",
273
- " label=\"Model Selection\",\n",
274
- " choices=list(MODEL_OPTIONS.keys()),\n",
275
- " value=\"OpenR1\"\n",
276
- " )\n",
277
- " input_media = gr.File(\n",
278
- " label=\"Upload Image\", type=\"filepath\"\n",
279
- " )\n",
280
- " text_input = gr.Textbox(label=\"Question\", placeholder=\"Ask a question about the image...\")\n",
281
- " submit_btn = gr.Button(value=\"Submit\", elem_classes=\"submit-btn\")\n",
282
- "\n",
283
- " with gr.Column():\n",
284
- " output_text = gr.Textbox(label=\"Output Text\", lines=10)\n",
285
- " plain_text_output = gr.Textbox(label=\"Standardized Plain Text\", lines=10)\n",
286
- "\n",
287
- " submit_btn.click(\n",
288
- " qwen_inference, [model_choice, input_media, text_input], [output_text]\n",
289
- " ).then(\n",
290
- " lambda output_text: format_plain_text(output_text), [output_text], [plain_text_output]\n",
291
- " )\n",
292
- "\n",
293
- " # Add examples directly usable by clicking\n",
294
- " with gr.Row():\n",
295
- " with gr.Column():\n",
296
- " line_spacing = gr.Dropdown(\n",
297
- " choices=[0.5, 1.0, 1.15, 1.5, 2.0, 2.5, 3.0],\n",
298
- " value=1.5,\n",
299
- " label=\"Line Spacing\"\n",
300
- " )\n",
301
- " font_size = gr.Dropdown(\n",
302
- " choices=[\"8\", \"10\", \"12\", \"14\", \"16\", \"18\", \"20\", \"22\", \"24\"],\n",
303
- " value=\"18\",\n",
304
- " label=\"Font Size\"\n",
305
- " )\n",
306
- " alignment = gr.Dropdown(\n",
307
- " choices=[\"Left\", \"Center\", \"Right\", \"Justified\"],\n",
308
- " value=\"Justified\",\n",
309
- " label=\"Text Alignment\"\n",
310
- " )\n",
311
- " image_size = gr.Dropdown(\n",
312
- " choices=[\"Small\", \"Medium\", \"Large\"],\n",
313
- " value=\"Small\",\n",
314
- " label=\"Image Size\"\n",
315
- " )\n",
316
- " file_format = gr.Radio([\"pdf\", \"docx\"], label=\"File Format\", value=\"pdf\")\n",
317
- " get_document_btn = gr.Button(value=\"Get Document\", elem_classes=\"download-btn\")\n",
318
- "\n",
319
- " get_document_btn.click(\n",
320
- " generate_document, [input_media, output_text, file_format, font_size, line_spacing, alignment, image_size], gr.File(label=\"Download Document\")\n",
321
- " )\n",
322
- "\n",
323
- "demo.launch(debug=True)"
324
- ]
325
- }
326
- ],
327
- "metadata": {
328
- "accelerator": "GPU",
329
- "colab": {
330
- "gpuType": "T4",
331
- "provenance": []
332
- },
333
- "kernelspec": {
334
- "display_name": "Python 3",
335
- "name": "python3"
336
- },
337
- "language_info": {
338
- "name": "python"
339
- }
340
- },
341
- "nbformat": 4,
342
- "nbformat_minor": 0
343
- }