Spaces:
Running
Running
Create gradio.ipynb
Browse files- gradio.ipynb +111 -0
gradio.ipynb
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"# Install Dependencies\n",
|
10 |
+
"!pip install gradio>=3.0 transformers>=4.25.0 torch>=1.12.0 gradio-theme-miku>=1.2.2"
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "code",
|
15 |
+
"execution_count": null,
|
16 |
+
"metadata": {},
|
17 |
+
"outputs": [],
|
18 |
+
"source": [
|
19 |
+
"# Import Libraries\n",
|
20 |
+
"import gradio as gr\n",
|
21 |
+
"from transformers import pipeline"
|
22 |
+
]
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cell_type": "code",
|
26 |
+
"execution_count": null,
|
27 |
+
"metadata": {},
|
28 |
+
"outputs": [],
|
29 |
+
"source": [
|
30 |
+
"# Load the JinaAI ReaderLM-v2 Model\n",
|
31 |
+
"model_name = \"jinaai/ReaderLM-v2\"\n",
|
32 |
+
"html_converter = pipeline(\"text-generation\", model=model_name)"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "code",
|
37 |
+
"execution_count": null,
|
38 |
+
"metadata": {},
|
39 |
+
"outputs": [],
|
40 |
+
"source": [
|
41 |
+
"# Function to Convert HTML to Markdown or JSON\n",
|
42 |
+
"def convert_html(html_input, output_format):\n",
|
43 |
+
" # Prepare the prompt for the model\n",
|
44 |
+
" prompt = f\"Convert the following HTML into {output_format}:\\n\\n{html_input}\"\n",
|
45 |
+
" \n",
|
46 |
+
" # Generate the output using the model\n",
|
47 |
+
" response = html_converter(prompt, max_length=500, num_return_sequences=1)\n",
|
48 |
+
" converted_output = response[0]['generated_text']\n",
|
49 |
+
" \n",
|
50 |
+
" # Extract the relevant part of the output (remove the prompt)\n",
|
51 |
+
" converted_output = converted_output.replace(prompt, \"\").strip()\n",
|
52 |
+
" return converted_output"
|
53 |
+
]
|
54 |
+
},
|
55 |
+
{
|
56 |
+
"cell_type": "code",
|
57 |
+
"execution_count": null,
|
58 |
+
"metadata": {},
|
59 |
+
"outputs": [],
|
60 |
+
"source": [
|
61 |
+
"# Create the Gradio Interface\n",
|
62 |
+
"iface = gr.Interface(\n",
|
63 |
+
" fn=convert_html, # Function to call\n",
|
64 |
+
" inputs=[\n",
|
65 |
+
" gr.Textbox(lines=10, placeholder=\"Paste your raw HTML here...\", label=\"Raw HTML Input\"),\n",
|
66 |
+
" gr.Radio([\"Markdown\", \"JSON\"], label=\"Output Format\", value=\"Markdown\")\n",
|
67 |
+
" ],\n",
|
68 |
+
" outputs=gr.Textbox(lines=10, label=\"Converted Output\"),\n",
|
69 |
+
" title=\"HTML to Markdown/JSON Converter\",\n",
|
70 |
+
" description=\"Convert raw HTML into beautifully formatted Markdown or JSON using JinaAI ReaderLM-v2.\",\n",
|
71 |
+
" theme=\"NoCrypt/miku\", # Apply the NoCrypt/miku theme\n",
|
72 |
+
" examples=[\n",
|
73 |
+
" [\"<h1>Hello World</h1><p>This is a <strong>test</strong>.</p>\", \"Markdown\"],\n",
|
74 |
+
" [\"<ul><li>Item 1</li><li>Item 2</li></ul>\", \"JSON\"]\n",
|
75 |
+
" ]\n",
|
76 |
+
")"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "code",
|
81 |
+
"execution_count": null,
|
82 |
+
"metadata": {},
|
83 |
+
"outputs": [],
|
84 |
+
"source": [
|
85 |
+
"# Launch the Interface\n",
|
86 |
+
"iface.launch(inline=True) # Embed the interface in the notebook"
|
87 |
+
]
|
88 |
+
}
|
89 |
+
],
|
90 |
+
"metadata": {
|
91 |
+
"kernelspec": {
|
92 |
+
"display_name": "Python 3",
|
93 |
+
"language": "python",
|
94 |
+
"name": "python3"
|
95 |
+
},
|
96 |
+
"language_info": {
|
97 |
+
"codemirror_mode": {
|
98 |
+
"name": "ipython",
|
99 |
+
"version": 3
|
100 |
+
},
|
101 |
+
"file_extension": ".py",
|
102 |
+
"mimetype": "text/x-python",
|
103 |
+
"name": "python",
|
104 |
+
"nbconvert_exporter": "python",
|
105 |
+
"pygments_lexer": "ipython3",
|
106 |
+
"version": "3.9.0"
|
107 |
+
}
|
108 |
+
},
|
109 |
+
"nbformat": 4,
|
110 |
+
"nbformat_minor": 5
|
111 |
+
}
|