Jan-Hendrik Müller commited on
Commit
f10a729
1 Parent(s): d14e8b4
Files changed (3) hide show
  1. app.py +8 -108
  2. hi.ipynb +90 -0
  3. requirements.txt +1 -2
app.py CHANGED
@@ -1,112 +1,12 @@
1
  import gradio as gr
2
- import bpy
3
- from tqdm import tqdm
4
- from math import pi
5
- import tempfile
6
- import molecularnodes as mn
7
- import os
8
 
9
- def enable_GPUS():
10
- bpy.data.scenes[0].render.engine = "CYCLES" #"CYCLES"
11
- # Set the device_type
12
- bpy.context.preferences.addons[
13
- "cycles"
14
- ].preferences.compute_device_type = "CUDA" # or "OPENCL"
15
 
16
- # Set the device and feature set
17
- bpy.context.scene.cycles.device = "GPU"
 
 
 
18
 
19
- for scene in bpy.data.scenes:
20
- scene.cycles.device = "GPU"
21
-
22
- bpy.context.preferences.addons["cycles"].preferences.get_devices()
23
- print(bpy.context.preferences.addons["cycles"].preferences.compute_device_type)
24
- for d in bpy.context.preferences.addons["cycles"].preferences.devices:
25
- d["use"] = True # Using all devices, include GPU and CPU
26
- print(d["name"])
27
-
28
-
29
- enable_GPUS()
30
-
31
- window = bpy.context.window
32
- screen = window.screen
33
-
34
- style = 'cartoon'
35
-
36
- nodes_to_append = ["MN_color_set",
37
- "MN_color_common",
38
- "MN_color_attribute_random",
39
- mn.nodes.styles_mapping[style]]
40
-
41
- def get_areas(type):
42
- return [area for area in screen.areas if area.type == type]
43
-
44
- def get_regions(areas):
45
- return [region for region in areas[0].regions if region.type == 'WINDOW']
46
-
47
- for node in nodes_to_append:
48
- bpy.ops.wm.append(
49
- 'INVOKE_DEFAULT',
50
- directory = os.path.join(mn.nodes.mn_data_file, 'NodeTree'),
51
- filename = node,
52
- link = False
53
- )
54
-
55
- def generate(progress=gr.Progress(track_tqdm=True)):
56
- area_type = 'VIEW_3D'
57
- areas = get_areas(area_type)
58
- with bpy.context.temp_override(window=window, area=areas[0], region=get_regions(areas)[0], screen=screen):
59
-
60
-
61
- for obj in bpy.context.scene.objects:
62
- if obj.type == 'MESH':
63
- bpy.data.objects.remove(obj, do_unlink=True)
64
-
65
- molecule = mn.load.molecule_rcsb("7TYG", starting_style=style, center_molecule=True)
66
- molecule.select_set(True)
67
- bpy.context.view_layer.objects.active = molecule
68
-
69
- bpy.ops.view3d.camera_to_view_selected()
70
- camera = bpy.data.objects["Camera"]
71
- camera.data.dof.use_dof = True
72
- camera.data.dof.focus_distance = 5
73
- camera.data.dof.aperture_fstop = 4
74
- camera.data.angle = pi / 3
75
- camera.data.type = "PERSP"
76
-
77
- with tempfile.NamedTemporaryFile(suffix=".JPEG", delete=False) as f:
78
- bpy.context.scene.render.resolution_y = 400
79
- bpy.context.scene.render.resolution_x = 600
80
- bpy.context.scene.render.image_settings.file_format = "JPEG"
81
- bpy.context.scene.render.filepath = f.name
82
-
83
- with tqdm() as pbar:
84
-
85
- def elapsed(dummy):
86
- pbar.update()
87
-
88
- bpy.app.handlers.render_stats.append(elapsed)
89
- bpy.context.scene.frame_set(1)
90
- bpy.context.scene.frame_current = 1
91
- bpy.ops.render.render(animation=False, write_still=True)
92
-
93
- bpy.data.images["Render Result"].save_render(
94
- filepath=bpy.context.scene.render.filepath
95
- )
96
- bpy.app.handlers.render_stats.clear()
97
- return f.name
98
-
99
- with gr.Blocks() as demo:
100
- with gr.Row():
101
- with gr.Column():
102
- render_btn = gr.Button("Render")
103
- with gr.Column(scale=3):
104
- image = gr.Image(type="filepath")
105
-
106
- render_btn.click(
107
- generate,
108
- outputs=[image],
109
- )
110
-
111
- demo.queue(concurrency_count=1)
112
- demo.launch(debug=True, inline=True)
 
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
+ def greet(name, intensity):
4
+ return "Hello22, " + name + "!" * int(intensity)
 
 
 
 
5
 
6
+ demo = gr.Interface(
7
+ fn=greet,
8
+ inputs=["text", "slider"],
9
+ outputs=["text"],
10
+ )
11
 
12
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
hi.ipynb ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stderr",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "/Users/jan-hendrik/projects/bpy-nvidia-T4/.venv/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
13
+ " from .autonotebook import tqdm as notebook_tqdm\n"
14
+ ]
15
+ },
16
+ {
17
+ "name": "stdout",
18
+ "output_type": "stream",
19
+ "text": [
20
+ "Running on local URL: http://127.0.0.1:7860\n",
21
+ "\n",
22
+ "To create a public link, set `share=True` in `launch()`.\n"
23
+ ]
24
+ },
25
+ {
26
+ "data": {
27
+ "text/html": [
28
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
29
+ ],
30
+ "text/plain": [
31
+ "<IPython.core.display.HTML object>"
32
+ ]
33
+ },
34
+ "metadata": {},
35
+ "output_type": "display_data"
36
+ },
37
+ {
38
+ "data": {
39
+ "text/plain": []
40
+ },
41
+ "execution_count": 1,
42
+ "metadata": {},
43
+ "output_type": "execute_result"
44
+ }
45
+ ],
46
+ "source": [
47
+ "import gradio as gr\n",
48
+ "\n",
49
+ "def greet(name, intensity):\n",
50
+ " return \"Hello, \" + name + \"!\" * int(intensity)\n",
51
+ "\n",
52
+ "demo = gr.Interface(\n",
53
+ " fn=greet,\n",
54
+ " inputs=[\"text\", \"slider\"],\n",
55
+ " outputs=[\"text\"],\n",
56
+ ")\n",
57
+ "\n",
58
+ "demo.launch()"
59
+ ]
60
+ },
61
+ {
62
+ "cell_type": "code",
63
+ "execution_count": null,
64
+ "metadata": {},
65
+ "outputs": [],
66
+ "source": []
67
+ }
68
+ ],
69
+ "metadata": {
70
+ "kernelspec": {
71
+ "display_name": ".venv",
72
+ "language": "python",
73
+ "name": "python3"
74
+ },
75
+ "language_info": {
76
+ "codemirror_mode": {
77
+ "name": "ipython",
78
+ "version": 3
79
+ },
80
+ "file_extension": ".py",
81
+ "mimetype": "text/x-python",
82
+ "name": "python",
83
+ "nbconvert_exporter": "python",
84
+ "pygments_lexer": "ipython3",
85
+ "version": "3.11.10"
86
+ }
87
+ },
88
+ "nbformat": 4,
89
+ "nbformat_minor": 2
90
+ }
requirements.txt CHANGED
@@ -1,2 +1 @@
1
- bpy
2
- molecularnodes
 
1
+ bpy